-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
95 lines (76 loc) · 2.94 KB
/
day11.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import math
from types import SimpleNamespace
from typing import List
def parse(input_data: str) -> List[SimpleNamespace]:
monkeys = []
for block in example_input.split('\n\n'):
monkey = SimpleNamespace(inspections=0)
for line in block.splitlines():
match line.split(': '):
case [' Starting items', items]:
monkey.items = list(map(int, items.split(', ')))
case [' Operation', operation]:
monkey.operation = operation.split(' = ')[1]
case [' Test', test]:
monkey.worry_divisor = int(test.split()[-1])
case [' If true', branch]:
monkey.branch_a = int(branch.split()[-1])
case [' If false', branch]:
monkey.branch_b = int(branch.split()[-1])
monkeys.append(monkey)
return monkeys
def solve(input_data: str, part: int) -> int:
monkeys = parse(input_data)
worry_reducer = math.prod(monkey.worry_divisor for monkey in monkeys)
rounds = 20 if part == 1 else 10000
for _ in range(rounds):
for monkey in monkeys:
for item in monkey.items:
old = item
item = eval(monkey.operation)
if part == 1:
item //= 3 # Part 1
else:
item %= worry_reducer # Part 2
recipient = monkeys[monkey.branch_b]
if item % monkey.worry_divisor == 0:
recipient = monkeys[monkey.branch_a]
recipient.items.append(item)
monkey.inspections += 1
monkey.items.clear()
monkey_business = math.prod(
sorted([monkey.inspections for monkey in monkeys])[-2:])
return monkey_business
if __name__ == "__main__":
example_input = (
'Monkey 0:\n'
' Starting items: 79, 98\n'
' Operation: new = old * 19\n'
' Test: divisible by 23\n'
' If true: throw to monkey 2\n'
' If false: throw to monkey 3\n'
'\n'
'Monkey 1:\n'
' Starting items: 54, 65, 75, 74\n'
' Operation: new = old + 6\n'
' Test: divisible by 19\n'
' If true: throw to monkey 2\n'
' If false: throw to monkey 0\n'
'\n'
'Monkey 2:\n'
' Starting items: 79, 60, 97\n'
' Operation: new = old * old\n'
' Test: divisible by 13\n'
' If true: throw to monkey 1\n'
' If false: throw to monkey 3\n'
'\n'
'Monkey 3:\n'
' Starting items: 74\n'
' Operation: new = old + 3\n'
' Test: divisible by 17\n'
' If true: throw to monkey 0\n'
' If false: throw to monkey 1\n')
expected_amount_1 = 10605
expected_amount_2 = 2713310158
assert solve(example_input, part=1) == expected_amount_1
assert solve(example_input, part=2) == expected_amount_2