-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
40 lines (34 loc) · 1.45 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import heapq
from math import prod, lcm
from copy import deepcopy
with open('input.txt') as file:
lines = [line.split('\n') for line in file.read().split('\n\n')]
monkeys = []
for line in lines:
temp_dict = {}
monkey = int(line[0].split(' ')[1][:-1])
temp_dict['items'] = list(map(int, line[1].split('Starting items: ')[1:].pop().split(', ')))
temp_dict['operation'] = line[2].split('Operation: new = ')[1:].pop()
temp_dict['test'] = int(line[3].split('Test: divisible by ')[1:].pop())
temp_dict['if_true'] = int(line[4].split('If true: throw to monkey ')[1:].pop())
temp_dict['if_false'] = int(line[5].split('If false: throw to monkey ')[1:].pop())
temp_dict['inspections'] = 0
monkeys.append(temp_dict)
div_by = lcm(*[i['test'] for i in monkeys])
def main(it):
monkeys_temp = deepcopy(monkeys)
for i in range(it):
for line in monkeys_temp:
for item in line['items']:
old = item
worry = eval(line['operation'])
if it == 20:
worry = worry // 3
elif it == 10_000:
worry = worry % div_by
monkeys_temp[line['if_true' if worry % line['test'] == 0 else 'if_false']]['items'].append(worry)
line['inspections'] += 1
line['items'] = []
return (i['inspections'] for i in monkeys_temp)
print(prod(heapq.nlargest(2, main(20))))
print(prod(heapq.nlargest(2, main(10_000))))