2016-12-10 - Balance Bots
(original .ipynb)from collections import defaultdict puzzle_input_str = open("puzzle_input/day10.txt").read() def parse_initial_value(line, values): val_str, dest = line.removeprefix("value ").split(" goes to ") val = int(val_str) values[dest].append(val) def parse_instruction(line, instructions): src, destinations = line.split(" gives ") low, high = destinations.split(" and ") instructions[src] = ( low.removeprefix("low to "), high.removeprefix("high to ") ) def parse_input(input_str): values = defaultdict(list) instructions = defaultdict(list) for line in input_str.split("\n"): if line.startswith("value"): parse_initial_value(line, values) else: parse_instruction(line, instructions) return values, instructions def is_output(dest): return dest.startswith("output") def next_bot(values): for bot in values: if is_output(bot): continue if len(values[bot]) == 2: return bot return None def solve(input_str): values, instructions = parse_input(input_str) bot = next_bot(values) p1 = None while not(bot is None): low_dest, high_dest = instructions[bot] low, high = sorted(values[bot]) if low == 17 and high == 61: p1 = bot.removeprefix("bot ") values[low_dest].append(low) values[high_dest].append(high) values[bot] = [] bot = next_bot(values) p2 = values["output 0"][0] * values["output 1"][0] * values["output 2"][0] return p1, p2 part_one, part_two = solve(puzzle_input_str) print("part one:", part_one) print("part two:", part_two)
part one: 98 part two: 4042