65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
from collections import defaultdict
|
|
from tools.aoc import AOCDay
|
|
from typing import Any
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
(2, "input10_test"),
|
|
(161, "input10"),
|
|
],
|
|
[
|
|
(133163, "input10"),
|
|
],
|
|
]
|
|
|
|
def parse_input(self, p2: bool = False) -> int | dict[int, int]:
|
|
looking_for = [17, 61] if not self.is_test() else [2, 5]
|
|
bots = defaultdict(list)
|
|
outputs = defaultdict(int)
|
|
comparing = []
|
|
for line in self.getInput():
|
|
p = line.split()
|
|
if p[0] == "value":
|
|
bots[int(p[5])].append(int(p[1]))
|
|
else:
|
|
comparing.append((int(p[1]), p[5] == "output", int(p[6]), p[10] == "output", int(p[11])))
|
|
|
|
while comparing:
|
|
leftover = []
|
|
for bot, low_out, low_id, high_out, high_id in comparing:
|
|
if len(bots[bot]) != 2:
|
|
leftover.append((bot, low_out, low_id, high_out, high_id))
|
|
continue
|
|
|
|
chips = list(sorted(bots[bot]))
|
|
if not p2 and chips == looking_for:
|
|
return bot
|
|
|
|
if low_out:
|
|
outputs[low_id] = chips[0]
|
|
else:
|
|
bots[low_id].append(chips[0])
|
|
|
|
if high_out:
|
|
outputs[high_id] = chips[-1]
|
|
else:
|
|
bots[high_id].append(chips[-1])
|
|
|
|
comparing = leftover
|
|
|
|
return outputs
|
|
|
|
def part1(self) -> Any:
|
|
return self.parse_input()
|
|
|
|
def part2(self) -> Any:
|
|
output = self.parse_input(p2=True)
|
|
return output[0] * output[1] * output[2]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
day = Day(2016, 10)
|
|
day.run(verbose=True)
|