39 lines
1.0 KiB
Python
39 lines
1.0 KiB
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:])
|
|
if "*" in op_line:
|
|
return lambda x: x*val
|
|
return lambda x: x+val
|
|
|
|
|
|
for line in lines:
|
|
monkeys.append({
|
|
"items": [*map(int, next(lines)[16:].split(", "))],
|
|
"op": create_op(next(lines)[17:]),
|
|
"div": int(next(lines)[19:]),
|
|
"if_true": int(next(lines)[25:]),
|
|
"if_false": int(next(lines)[26:]),
|
|
"count": 0
|
|
})
|
|
next(lines, None)
|
|
|
|
divisor = reduce(lambda x, y: x*y, set(monkey['div'] for monkey in monkeys))
|
|
|
|
for _ in range(10000):
|
|
for monkey in monkeys:
|
|
while monkey['items']:
|
|
monkey['count'] += 1
|
|
worry = monkey['op'](monkey['items'].pop(0)) % divisor
|
|
monkeys[monkey['if_true' if worry % monkey['div'] == 0 else 'if_false']]['items'].append(worry)
|
|
|
|
insp = sorted(monkey['count'] for monkey in monkeys)
|
|
print(insp[-2]*insp[-1])
|