-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday12.py
50 lines (41 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
from collections import defaultdict, Counter
from typing import Dict, List
def get_caves() -> Dict[str, List[str]]:
lines = [line.strip().split('-') for line in open('2021//input//day12.txt').readlines()]
caves = defaultdict(list)
for p1, p2 in lines:
if p1 == "start":
caves[p1].append(p2)
elif p2 == "end":
caves[p1].append(p2)
elif p1 == "end":
caves[p2].append(p1)
else:
caves[p1].append(p2)
caves[p2].append(p1)
return caves
def get_paths(caves, path_so_far, target):
paths = []
for cave in caves[path_so_far[-1]]:
if cave == target:
paths.append(path_so_far + [cave])
elif cave.isupper() or cave not in path_so_far:
paths += get_paths(caves, path_so_far + [cave], target)
return paths
def get_paths2(caves, path_so_far, target, has_double=False):
paths = []
for cave in caves[path_so_far[-1]]:
if cave == target:
paths.append(path_so_far + [cave])
elif cave.isupper() or cave not in path_so_far:
paths += get_paths2(caves, path_so_far + [cave], target, has_double)
elif has_double:
continue
else:
paths += get_paths2(caves, path_so_far + [cave], target, True)
return paths
caves = get_caves()
paths = get_paths(caves, ['start'], 'end')
print("Part 1:", len(paths))
paths = get_paths2(caves, ['start'], 'end')
print("Part 2:", len(paths))