-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
56 lines (40 loc) · 1.47 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
53
54
55
56
from collections import defaultdict
with open("./input12.txt") as file:
graph = defaultdict(list)
for line in file:
start, end = line.strip().split("-")
graph[start].append(end)
graph[end].append(start)
def find_path(graph, start, small_visits=1):
incomplete = [[start]]
complete = []
while incomplete:
current_path = incomplete.pop()
current_cave = current_path[-1]
for cave in graph[current_cave]:
if cave == "end":
found = current_path + [cave]
complete.append(found)
continue
if cave.islower():
if cave == "start":
continue
visits = current_path.count(cave)
if visits >= small_visits or will_have_extra_small_cave_visits(current_path, cave):
continue
to_check = current_path + [cave]
incomplete.append(to_check)
return complete
def will_have_extra_small_cave_visits(path, small_cave):
smalls = [cave for cave in path if cave.islower()]
twos = 0
counts = defaultdict(int)
counts[small_cave] += 1
for cave in smalls:
counts[cave] += 1
return sum(count > 1 for count in counts.values()) > 1
if __name__ == "__main__":
paths = find_path(graph, "start")
print(f"Part 1\nThere are {len(paths)} paths.")
paths = find_path(graph, "start", 2)
print(f"Part 2\nThere are {len(paths)} paths.")