Skip to content

Commit b73087e

Browse files
committed
Shamefully solve day 10 of 2016 in Python.
1 parent c54656e commit b73087e

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

2016/10.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import sys
2+
3+
4+
class Problem:
5+
def __init__(self):
6+
self._map = {}
7+
8+
def _add_out_node_if_needed(self, x):
9+
if x.startswith("output"):
10+
self._map[x] = {"ins": []}
11+
12+
def add_line(self, line: str):
13+
if line.startswith("value"):
14+
v, target = line.split(" goes to ")
15+
self._map[v] = {"in": int(v.replace("value ", "")),
16+
"out": target}
17+
self._add_out_node_if_needed(target)
18+
if line.startswith("bot"):
19+
b, targets = line.split(" gives low to ")
20+
lo, hi = targets.split(" and high to ")
21+
self._map[b] = {"out": {"lo": lo, "hi": hi},
22+
"ins": []}
23+
self._add_out_node_if_needed(lo)
24+
self._add_out_node_if_needed(hi)
25+
26+
def _needs_processing(self, x):
27+
if not x.startswith("bot"):
28+
return False
29+
return len(self._map[x]["ins"]) == 2
30+
31+
def solve(self):
32+
queue = [k for k in self._map.keys() if k.startswith("value")]
33+
while queue:
34+
elem, queue = queue[0], queue[1:]
35+
info = self._map[elem]
36+
if elem.startswith("value"):
37+
target = info["out"]
38+
self._map[target]["ins"].append(info["in"])
39+
if self._needs_processing(target):
40+
queue.append(target)
41+
if elem.startswith("bot"):
42+
vlo, vhi = sorted(info["ins"])
43+
lo = info["out"]["lo"]
44+
hi = info["out"]["hi"]
45+
self._map[lo]["ins"].append(vlo)
46+
self._map[hi]["ins"].append(vhi)
47+
if self._needs_processing(lo):
48+
queue.append(lo)
49+
if self._needs_processing(hi):
50+
queue.append(hi)
51+
52+
def find_bot_with(self, ins) -> str:
53+
for name, info in self._map.items():
54+
if not name.startswith("bot"):
55+
continue
56+
if ins == sorted(info["ins"]):
57+
return name
58+
return "NONE"
59+
60+
def get_output(self, x) -> int:
61+
return self._map[f"output {x}"]["ins"][0]
62+
63+
64+
if __name__ == "__main__":
65+
p = Problem()
66+
for line in sys.stdin.readlines():
67+
p.add_line(line.strip())
68+
p.solve()
69+
print(p._map)
70+
print(p.find_bot_with([17, 61]))
71+
print(p.get_output(0) * p.get_output(1) * p.get_output(2))

0 commit comments

Comments
 (0)