-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11a.py
More file actions
31 lines (22 loc) · 702 Bytes
/
day11a.py
File metadata and controls
31 lines (22 loc) · 702 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
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
count = defaultdict(int)
count['you'] = 1
q = [node for node in nodes if in_edges[node] == 0]
while q:
curr = q.pop()
for neigh in neighs[curr]:
in_edges[neigh] -= 1
count[neigh] += count[curr]
if in_edges[neigh] == 0:
q.append(neigh)
print(count['out'])