-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11b.py
More file actions
38 lines (28 loc) · 943 Bytes
/
day11b.py
File metadata and controls
38 lines (28 loc) · 943 Bytes
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
from collections import defaultdict
f = open('input.txt', 'r')
lines = [line.split(' ') for line in f.read().split('\n')]
neighs = defaultdict(list)
neighs |= {line[0][:-1]: line[1:] for line in lines}
nodes = list(set(list(neighs.keys())))
nodes = list(set(nodes + [node for vals in neighs.values() for node in vals]))
in_edges = defaultdict(int)
for key, values in neighs.items():
for neigh in values:
in_edges[neigh] += 1
def paths(start, end):
in_e = defaultdict(int)
in_e |= {k:v for k,v in in_edges.items()}
count = defaultdict(int)
count[start] = 1
q = [node for node in nodes if in_e[node] == 0]
while q:
curr = q.pop()
for neigh in neighs[curr]:
in_e[neigh] -= 1
count[neigh] += count[curr]
if in_e[neigh] == 0:
q.append(neigh)
return count[end]
res = paths('svr', 'fft') * paths('fft', 'dac') * paths('dac', 'out')
res += paths('svr', 'dac') * paths('dac', 'fft') * paths('fft', 'out')
print(res)