-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday12-memo.py
33 lines (26 loc) · 1 KB
/
day12-memo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# solution with memoization
# and just the number of paths instead of the full paths
# based on a solution from axr123 on reddit
from collections import defaultdict
from functools import lru_cache
edges = defaultdict(list)
for line in open('2021//input//day12.txt').readlines():
src, dst = map(lambda s: s.strip(), line.split('-'))
edges[src].append(dst)
edges[dst].append(src)
@lru_cache(maxsize=None)
def dfs(current, visited, twice=True) -> int:
num_paths = 0
for dst in edges[current]:
if dst == "end":
num_paths += 1
elif not dst.islower():
num_paths += dfs(dst, tuple(set(visited)), twice)
elif dst != "start":
if dst not in visited:
num_paths += dfs(dst, tuple(set(visited) | {dst}), twice)
elif not twice:
num_paths += dfs(dst, tuple(set(visited) | {dst}), True)
return num_paths
print("Part 1:", dfs("start", tuple(set())))
print("Part 2:", dfs("start", tuple(set()), twice=False))