-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
57 lines (50 loc) · 1.5 KB
/
solution.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
import time
def prepare_graph(input):
adjacencies = {}
lines = (l.strip() for l in input)
for l in lines:
a, b = l.split("-")
if a in adjacencies.keys():
adjacencies[a].append(b)
else:
adjacencies[a] = [b]
if b in adjacencies.keys():
adjacencies[b].append(a)
else:
adjacencies[b] = [a]
return adjacencies
def visit(node, visited, adj, twice):
paths = []
visited.append(node)
if node == "end":
paths.append(visited)
else:
for n in adj[node]:
if n.islower() and n in visited:
if n != "start" and not twice:
paths += visit(n, visited.copy(), adj, True)
else:
continue
else:
paths += visit(n, visited.copy(), adj, twice)
return paths
def part1(input):
adj = prepare_graph(input)
paths = visit("start", [], adj, True)
return len(paths)
def part2(input):
adj = prepare_graph(input)
paths = visit("start", [], adj, False)
return len(paths)
def main():
with open("day12/input.txt") as f:
input = f.readlines()
t0 = time.perf_counter_ns()
print(f"solution for part 1 is: {part1(input)}")
t1 = time.perf_counter_ns()
print(f"part 1 took {t1-t0} ns")
print(f"solution for part 2 is: {part2(input)}")
t2 = time.perf_counter_ns()
print(f"part 2 took {t2-t1} ns")
if __name__ == "__main__":
main()