Files
python-aoc-2022/day11/part2.py
Sebastian Seedorf bdbf95b769 Day 11
2022-12-11 20:22:25 +01:00

37 lines
963 B
Python

#!/usr/bin/env python3
from functools import reduce
lines = (x.strip() for x in open("input.txt"))
monkeys = []
def create_op(op_line):
if "old * old" in op_line:
return lambda x: x**2
val = int(op_line[6:])
return (lambda x: x*val) if "*" in op_line else (lambda x: x+val)
for line in lines:
monkeys.append((
[*map(int, next(lines)[16:].split(", "))],
create_op(next(lines)[17:]),
int(next(lines)[19:]),
int(next(lines)[25:]),
int(next(lines)[26:]),
[0]
))
next(lines, None)
mod = reduce(lambda x, y: x*y, set(monkey[2] for monkey in monkeys))
for _ in range(10000):
for items, op, div, if_true, if_false, count in monkeys:
while items:
count[0] += 1
worry = op(items.pop(0)) % mod
monkeys[if_true if worry % div == 0 else if_false][0].append(worry)
insp = sorted(monkey[-1][0] for monkey in monkeys)
print(insp[-2]*insp[-1])