-
Notifications
You must be signed in to change notification settings - Fork 0
/
2022_11_monkeys.py
78 lines (61 loc) · 2.23 KB
/
2022_11_monkeys.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import operator
import heapq
import math
class Monkey:
def __init__(self, starting_items, operation, test_value, true_monkey, false_monkey):
self.items = starting_items
self.operation = operation
self.test_value = test_value
self.test = make_test(test_value)
self.true_monkey = true_monkey
self.false_monkey = false_monkey
self.inspection_count = 0
def take_turn(self):
for item in self.items:
self.inspection_count += 1
item = self.operation(item)
item %= mod
if self.test(item):
monkeys[self.true_monkey].items.append(item)
else:
monkeys[self.false_monkey].items.append(item)
self.items.clear()
operators = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
}
def make_operation(left, symbol, right):
operator_function = operators[symbol]
left = None if left == 'old' else int(left)
right = None if right == 'old' else int(right)
def operation(old):
return operator_function(old if left is None else left, old if right is None else right)
return operation
def make_test(test_value):
def test(value):
return not value % test_value
return test
monkeys = []
lines = (line.strip() for line in open("2022_11_monkeys_input.txt"))
for line in lines:
if not line:
continue
line = next(lines)
starting_items = [int(item)
for item in line.split("Starting items: ", 1)[1].split(', ')]
line = next(lines)
operation = make_operation(*line.split("Operation: new = ", 1)[1].split())
line = next(lines)
test_value = int(line.split("Test: divisible by ", 1)[1])
line = next(lines)
true_monkey = int(line.split("If true: throw to monkey ", 1)[1])
line = next(lines)
false_monkey = int(line.split("If false: throw to monkey ", 1)[1])
monkeys.append(Monkey(starting_items, operation,
test_value, true_monkey, false_monkey))
mod = math.prod(monkey.test_value for monkey in monkeys)
for _ in range(10000):
for monkey in monkeys:
monkey.take_turn()
print(operator.mul(*heapq.nlargest(2, (monkey.inspection_count for monkey in monkeys))))