Skip to content

Instantly share code, notes, and snippets.

@p7g
Created December 12, 2021 16:56
Show Gist options
  • Save p7g/523b2f7e9caf4c797126402831d3e040 to your computer and use it in GitHub Desktop.
Save p7g/523b2f7e9caf4c797126402831d3e040 to your computer and use it in GitHub Desktop.
the after takes less than double the time of the before
diff --git a/12.py b/12.py
index 8cb48a8..e6097ae 100644
--- a/12.py
+++ b/12.py
@@ -8,39 +8,39 @@ for line in data.splitlines():
G.add_edge(a, b)
-def find_paths(G, so_far, visited, from_="start"):
+def find_paths(G, so_far, from_="start"):
edges = G[from_]
for node in edges:
if node == "end":
yield [*so_far, "end"]
- elif node.islower() and node in visited:
+ elif node.islower() and node in so_far:
continue
else:
- yield from find_paths(G, [*so_far, node], {*visited, node}, from_=node)
+ yield from find_paths(G, [*so_far, node], from_=node)
-print(len(list(find_paths(G, ["start"], {"start"}))))
+print(len(list(find_paths(G, ["start"]))))
-def find_paths(G, so_far, visited, from_="start", twice=False):
+def find_paths(G, so_far, from_="start"):
edges = G[from_]
+ smalls = [n for n in so_far if n.islower()]
+ twice = len(set(smalls)) != len(smalls)
for node in edges:
if node == "start":
continue
elif node == "end":
yield [*so_far, "end"]
- elif node.islower() and node in visited and twice:
+ elif node.islower() and node in so_far and twice:
continue
else:
yield from find_paths(
G,
[*so_far, node],
- {*visited, node},
from_=node,
- twice=twice or (node.islower() and node in visited),
)
-print(len(list(find_paths(G, ["start"], {}))))
+print(len(list(find_paths(G, ["start"]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment