-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.py
65 lines (51 loc) · 1.5 KB
/
10.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
from lib import *
input = read_input(2016, 10)
values = {}
fw = {}
for line in map(str.split, input.splitlines()):
if line[0] == "bot":
src, dst1, dst2 = map(int, [line[1], line[6], line[11]])
fw[src] = [dst1, dst2]
else:
val, dst = map(int, [line[1], line[5]])
values.setdefault(dst, []).append(val)
queue = []
for k, v in values.items():
if len(v) == 2:
queue.append((k, *sorted(v)))
while queue:
p, l, h = queue.pop()
if (l, h) == (17, 61):
print(p)
break
values.setdefault(fw[p][0], []).append(l)
values.setdefault(fw[p][1], []).append(h)
for k in fw[p]:
v = values[k]
if len(v) == 2:
queue.append((k, *sorted(v)))
values = {}
fw = {}
out = {}
for line in map(str.split, input.splitlines()):
if line[0] == "bot":
src, dst1, dst2 = map(int, [line[1], line[6], line[11]])
fw[src] = [(dst1, line[5] == "output"), (dst2, line[10] == "output")]
else:
val, dst = map(int, [line[1], line[5]])
[values, out][line[4] == "output"].setdefault(dst, []).append(val)
queue = []
for k, v in values.items():
if len(v) == 2:
queue.append((k, *sorted(v)))
while queue:
p, l, h = queue.pop()
for val, (k, o) in zip([l, h], fw[p]):
if o:
out[k] = val
continue
values.setdefault(k, []).append(val)
v = values[k]
if len(v) == 2:
queue.append((k, *sorted(v)))
print(out[0] * out[1] * out[2])