Solved problem 10 in Python

This commit is contained in:
David Doblas Jiménez 2022-05-11 16:02:17 +02:00
parent 1ae2df34d4
commit 4c5d8db1a7
1 changed files with 26 additions and 4 deletions

View File

@ -43,14 +43,15 @@
# Based on your instructions, what is the number of the bot that is responsible
# for comparing value-61 microchips with value-17 microchips?
import collections
import re
from copy import copy
from collections import defaultdict
from math import prod
with open("files/P10.txt") as f:
instructions = [line for line in f.read().strip().split("\n")]
bots_dic = collections.defaultdict(list)
bots_dic: dict[int, list] = defaultdict(list)
outputs_dic: dict[int, list] = defaultdict(list)
pipeline = {}
for line in instructions:
@ -63,7 +64,7 @@ for line in instructions:
pipeline[bot] = (given_to[0], low), (given_to[1], high)
def part_1():
def part_1() -> None:
found = False
while not found:
for bot, value in dict(bots_dic).items():
@ -78,5 +79,26 @@ def part_1():
bots_dic[high].append(high_val)
# --- Part Two ---
# What do you get if you multiply together the values of one chip in each of
# outputs 0, 1, and 2?
def part_2() -> None:
types = {"bot": bots_dic, "output": outputs_dic}
while bots_dic:
for bot, value in dict(bots_dic).items():
if len(value) == 2:
low_val, high_val = sorted(bots_dic.pop(bot))
(t1, low), (t2, high) = pipeline[bot]
types[t1][low].append(low_val)
types[t2][high].append(high_val)
res = prod(outputs_dic[k][0] for k in [0, 1, 2])
print(f"The answer is {res}")
if __name__ == "__main__":
part_1()
part_2()