-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
136 lines (111 loc) · 2.92 KB
/
solver.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
from pathlib import Path
from collections import defaultdict
currdir = Path(__file__).parent.absolute()
# paste the example from the problem here ↓
test_input = """
start-A
start-b
A-c
A-b
b-d
A-end
b-end
""".strip()
test_input2 = """
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc
""".strip()
test_input3 = """
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW
""".strip()
# part 1
def walk_path(connections, start="start", end="end"):
if start == end:
return 1
if start not in connections:
return 0
next_points = connections[start]
connections = {a: b for a, b in connections.items() if a != start or a.isupper()}
paths = 0
for point in next_points:
paths += walk_path(connections, point)
return paths
# part 2
def walk_unique_path(connections, unique_paths, path="start", start="start", end="end"):
if start == end:
unique_paths.add(path)
return 1
if start not in connections:
return 0
next_points = connections[start]
connections = {a: b for a, b in connections.items() if a != start or a.isupper()}
paths = 0
for point in next_points:
new_path = f"{path}-{point}" if point[0] != "_" else f"{path}-{point[1:-1]}"
paths += walk_unique_path(connections, unique_paths, new_path, point)
return paths
def part1(p_input):
connections = defaultdict(list)
for a, b in p_input:
connections[a].append(b)
connections[b].append(a)
solution = walk_path(connections=connections)
return solution
def part2(p_input):
connections, small_caves = defaultdict(list), set()
for a, b in p_input:
connections[a].append(b)
connections[b].append(a)
if a == "start" or a == "end" or b == "start" or b == "end":
continue
if a.islower():
small_caves.add(a)
if b.islower():
small_caves.add(b)
unique_paths = set()
walk_unique_path(connections, unique_paths) # init unique_paths with base case
for small_cave in small_caves:
new_connections = {a: b for a, b in connections.items()}
# duplicate one small cave
new_connections[f"_{small_cave}_"] = [x for x in new_connections[small_cave]]
for c, v in new_connections.items():
if small_cave in v:
new_connections[c].append(f"_{small_cave}_")
walk_unique_path(new_connections, unique_paths)
return len(unique_paths)
def main():
raw_input = open(currdir.joinpath("input.txt")).read()
# raw_input = test_input # testing with the example - comment for real input
connections = [line.split("-") for line in raw_input.splitlines()]
print("Solution to Part 1:")
print(part1(connections))
print("Solution to Part 2:")
print(part2(connections))
if __name__ == "__main__":
main()