-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.py
71 lines (61 loc) · 2.32 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
66
67
68
69
70
71
import sys
class Problem:
def __init__(self):
self._map = {}
def _add_out_node_if_needed(self, x):
if x.startswith("output"):
self._map[x] = {"ins": []}
def add_line(self, line: str):
if line.startswith("value"):
v, target = line.split(" goes to ")
self._map[v] = {"in": int(v.replace("value ", "")),
"out": target}
self._add_out_node_if_needed(target)
if line.startswith("bot"):
b, targets = line.split(" gives low to ")
lo, hi = targets.split(" and high to ")
self._map[b] = {"out": {"lo": lo, "hi": hi},
"ins": []}
self._add_out_node_if_needed(lo)
self._add_out_node_if_needed(hi)
def _needs_processing(self, x):
if not x.startswith("bot"):
return False
return len(self._map[x]["ins"]) == 2
def solve(self):
queue = [k for k in self._map.keys() if k.startswith("value")]
while queue:
elem, queue = queue[0], queue[1:]
info = self._map[elem]
if elem.startswith("value"):
target = info["out"]
self._map[target]["ins"].append(info["in"])
if self._needs_processing(target):
queue.append(target)
if elem.startswith("bot"):
vlo, vhi = sorted(info["ins"])
lo = info["out"]["lo"]
hi = info["out"]["hi"]
self._map[lo]["ins"].append(vlo)
self._map[hi]["ins"].append(vhi)
if self._needs_processing(lo):
queue.append(lo)
if self._needs_processing(hi):
queue.append(hi)
def find_bot_with(self, ins) -> str:
for name, info in self._map.items():
if not name.startswith("bot"):
continue
if ins == sorted(info["ins"]):
return name
return "NONE"
def get_output(self, x) -> int:
return self._map[f"output {x}"]["ins"][0]
if __name__ == "__main__":
p = Problem()
for line in sys.stdin.readlines():
p.add_line(line.strip())
p.solve()
print(p._map)
print(p.find_bot_with([17, 61]))
print(p.get_output(0) * p.get_output(1) * p.get_output(2))