36 lines
972 B
Python
36 lines
972 B
Python
#!/usr/bin/env python3
|
|
|
|
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)
|
|
|
|
for _ in range(20):
|
|
for idx, monkey in enumerate(monkeys):
|
|
while monkey['items']:
|
|
monkey['count'] += 1
|
|
worry = monkey['op'](monkey['items'].pop(0)) // 3
|
|
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])
|