Browse Source

Day 12: Keep track of duplication using boolean

Frans Bergman 3 years ago
parent
commit
e5240af015
1 changed files with 2 additions and 11 deletions
  1. 2 11
      12.py

+ 2 - 11
12.py

@@ -21,18 +21,9 @@ def find_paths_1(current, prev):
 
 print(find_paths_1("start", ["start"]))
 
-def ndup(ls):
-    curr = []
-    dups = 0
-    for l in ls:
-        if l in curr and l.islower():
-            dups += 1
-        curr.append(l)
-    return dups
-
-def find_paths_2(current, prev):
+def find_paths_2(current, prev, duped=False):
     if current == "end":
         return 1
-    return sum([find_paths_2(next, prev + [current]) for next in neighbour_map[current] if not ((next.islower() and next in prev and ndup(prev + [current]) > 0) or next == "start")])
+    return sum([find_paths_2(next, prev + [current], duped or (next.islower() and next in prev)) for next in neighbour_map[current] if not ((duped and next.islower() and (next in prev)) or next == "start")])
 
 print(find_paths_2("start", []))