-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.py
169 lines (144 loc) · 4.99 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# https://adventofcode.com/2022/day/11
from dataclasses import dataclass, field
import re
from typing import Callable
SAMPLE = """\
Monkey 0:
Starting items: 79, 98
Operation: new = old * 19
Test: divisible by 23
If true: throw to monkey 2
If false: throw to monkey 3
Monkey 1:
Starting items: 54, 65, 75, 74
Operation: new = old + 6
Test: divisible by 19
If true: throw to monkey 2
If false: throw to monkey 0
Monkey 2:
Starting items: 79, 60, 97
Operation: new = old * old
Test: divisible by 13
If true: throw to monkey 1
If false: throw to monkey 3
Monkey 3:
Starting items: 74
Operation: new = old + 3
Test: divisible by 17
If true: throw to monkey 0
If false: throw to monkey 1
"""
@dataclass
class Monkey:
items: list[int] = field(default_factory=list)
op: Callable[int, int] = None
divisor: int = 1
true_to: int = 0
false_to: int = 0
items_inspected: int = 0
@classmethod
def from_text(cls, text):
monkey = cls()
for line in text.splitlines():
line = line.strip()
if line.startswith("Starting items:"):
monkey.items = list(map(int, re.findall(r"\d+", line)))
elif line.startswith("Operation:"):
op, operand = line.split()[-2:]
if operand == "old":
assert op == "*"
monkey.op = lambda old: old * old
else:
if op == "+":
monkey.op = lambda old, num=int(operand): old + num
else:
assert op == "*"
monkey.op = lambda old, num=int(operand): old * num
elif line.startswith("Test:"):
monkey.divisor = int(line.split()[-1])
elif line.startswith("If true:"):
monkey.true_to = int(line.split()[-1])
elif line.startswith("If false:"):
monkey.false_to = int(line.split()[-1])
return monkey
def turn(self, monkeys, msg, part=1, global_mod=0):
for item in self.items:
#msg(f"Inspects {item}")
self.items_inspected += 1
item = self.op(item)
#msg(f" Worry goes to {item}")
if part == 1:
item //= 3
#msg(f" Divided by 3 to {item}")
else:
item %= global_mod
tf = (item % self.divisor == 0)
#msg(f" Divisible by {self.divisor}? {tf}")
goes_to = self.true_to if tf else self.false_to
#msg(f" Throw to {goes_to}")
monkeys[goes_to].items.append(item)
self.items = []
@dataclass
class Monkeys:
monkeys: list[Monkey] = field(default_factory=list)
global_mod: int = 1
@classmethod
def from_text(cls, text):
monkeys = cls()
for chunk in text.split("\n\n"):
monkeys.monkeys.append(Monkey.from_text(chunk))
for monkey in monkeys.monkeys:
monkeys.global_mod *= monkey.divisor
return monkeys
def __len__(self):
return len(self.monkeys)
def __getitem__(self, n):
return self.monkeys[n]
def round(self, msg=None, part=1):
if msg is None:
msg = lambda text: None
for i, monkey in enumerate(self.monkeys):
#msg(f"Monkey {i}")
monkey.turn(self, msg, part=part, global_mod=self.global_mod)
#msg(f"")
def all_items(self):
return [m.items for m in self.monkeys]
def test_parse_one_monkey():
monkey = Monkey.from_text(SAMPLE.split("\n\n")[0])
assert monkey.items == [79, 98]
assert monkey.op(10) == 190
assert monkey.divisor == 23
assert monkey.true_to == 2
assert monkey.false_to == 3
def test_parse_monkeys():
monkeys = Monkeys.from_text(SAMPLE)
assert monkeys.all_items() == [[79, 98], [54, 65, 75, 74], [79, 60, 97], [74]]
def test_turn():
monkeys = Monkeys.from_text(SAMPLE)
monkeys.round()
assert monkeys.all_items() == [[20, 23, 27, 26], [2080, 25, 167, 207, 401, 1046], [], []]
for _ in range(19):
monkeys.round()
assert monkeys.all_items() == [[10, 12, 14, 26, 34], [245, 93, 53, 199, 115], [], []]
def part1(text):
monkeys = Monkeys.from_text(text)
for _ in range(20):
monkeys.round()
by_activty = sorted(monkeys.monkeys, key=lambda m: m.items_inspected)
return by_activty[-1].items_inspected * by_activty[-2].items_inspected
def test_part1():
assert part1(SAMPLE) == 10605
if __name__ == "__main__":
text = open("day11.txt").read()
print(f"Part 1: {part1(text)}")
def part2(text):
monkeys = Monkeys.from_text(text)
for _ in range(10000):
monkeys.round(part=2)
by_activty = sorted(monkeys.monkeys, key=lambda m: m.items_inspected)
return by_activty[-1].items_inspected * by_activty[-2].items_inspected
def test_part2():
assert part2(SAMPLE) == 2713310158
if __name__ == "__main__":
text = open("day11.txt").read()
print(f"Part 2: {part2(text)}")