-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
52 lines (37 loc) · 1.26 KB
/
day12.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
from collections import defaultdict
def parse_data():
graph = defaultdict(list)
with open("./input") as f:
for line in f.readlines():
src, dst = line.strip().split("-")
graph[src].append(dst)
graph[dst].append(src)
return graph
def solve(graph, p2):
ans = 0
stack = list([("start", set(["start"]), "")])
while stack:
cave, visited_smalls, twice_small = stack.pop()
if cave == "end":
ans += 1
continue
for next_cave in graph[cave]:
if next_cave not in visited_smalls:
new_smalls = set(visited_smalls)
if next_cave.islower():
new_smalls.add(next_cave)
stack.append((next_cave, new_smalls, twice_small))
elif next_cave in visited_smalls:
if p2:
if next_cave not in ["start", "end"] and twice_small == "":
stack.append((next_cave, visited_smalls, next_cave))
else:
continue
return ans
def main():
graph = parse_data()
print(f"part1: {solve(graph, False)}")
print(f"part2: {solve(graph, True)}")
if __name__ == "__main__":
main()