-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
64 lines (49 loc) · 2.11 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
import collections
import math
import re
Monkey = collections.namedtuple('Monkey',
['name', 'items', 'operation', 'test', 'ifTrue', 'ifFalse', 'inspected_count'])
def get_line():
global input_file
return input_file.readline().strip()
def get_first_matched_group(regex) -> str:
return re.match(regex, get_line()).groups()[0]
def parse_file_to_monkeys() -> list[Monkey]:
global monkeys
monkeys = []
while line := get_line():
monkey = Monkey(
name=line.strip().lower(),
items=list(map(int, get_first_matched_group('^Starting items: ([0-9,\\w].*)$').split(', '))),
operation=get_first_matched_group('^Operation: new = (.*)$'),
test=int(get_first_matched_group('^Test: divisible by (\\d*)$')),
ifTrue=int(get_first_matched_group('^If true: throw to monkey (\\d*)$')),
ifFalse=int(get_first_matched_group('^If false: throw to monkey (\\d*)$')),
inspected_count=[0],
)
monkeys.append(monkey)
get_line()
return monkeys
def do_monkey_business(stress_level_updater):
for monkey in monkeys:
item_count = len(monkey.items)
monkey.inspected_count[0] += item_count
for i in range(0, item_count):
old = monkey.items.pop(0)
stress = int(stress_level_updater(eval(monkey.operation)))
test_outcome = monkey.ifTrue if stress % monkey.test == 0 else monkey.ifFalse
monkeys[test_outcome].items.append(stress)
def solve(rounds, stress_management):
global monkeys
for i in range(0, rounds):
do_monkey_business(stress_management)
inspected_counts = [monkey.inspected_count[0] for monkey in monkeys]
inspected_counts.sort(reverse=True)
return inspected_counts[0] * inspected_counts[1]
input_file = open('input-11-1.txt', 'r')
parse_file_to_monkeys()
print(f'solution 1={solve(20, lambda x: x / 3)}')
input_file = open('input-11-1.txt', 'r')
parse_file_to_monkeys()
super_modulo = math.prod([monkey.test for monkey in monkeys])
print(f'solution 2={solve(10000, lambda x: x % super_modulo)}')