-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_11.py
78 lines (61 loc) · 2.41 KB
/
day_11.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
from util import Test, Answer, ReadPuzzle, ReadExamplePuzzle
from re import findall
from copy import copy
class Monkey:
def __init__(self, name, items, operation, test, on_true, on_false):
self.items = list(int(v) for v in items.split(", "))
self.operation = operation
self.test = int(test)
self.on_true = int(on_true)
self.on_false = int(on_false)
self.inspections = 0
def inspectall1(self, monkeys):
self.inspections += len(self.items)
for old in self.items:
new = eval(self.operation, {'old': old})
new = int(new / 3)
if new % self.test == 0:
monkeys[self.on_true].items.append(new)
else:
monkeys[self.on_false].items.append(new)
self.items = []
def inspectall2(self, monkeys):
self.inspections += len(self.items)
for old in self.items:
new = eval(self.operation, {'old': old})
if new % self.test == 0:
monkeys[self.on_true].items.append(new)
else:
monkeys[self.on_false].items.append(new)
self.items = []
def __repr__(self):
return str(self.items)
def part1(data):
for i in range(20):
for monkey in data:
monkey.inspectall1(data)
data.sort(key=lambda self: self.inspections, reverse=True)
return data[0].inspections * data[1].inspections
def part2(data):
for i in range(10000):
for monkey in data:
monkey.inspectall2(data)
data.sort(key=lambda self: self.inspections, reverse=True)
return data[0].inspections * data[1].inspections
def preprocess_data(data):
"""Do any additional parsing to the data to prep for answers"""
matches = findall(
r"Monkey (\d+):\s+Starting items: ([\d ,]+)\s+Operation: new = (.+?)\s+Test: divisible by (\d+)\s+If true: throw to monkey (\d+)\s+If false: throw to monkey (\d+)",
"\n".join(data))
return list(Monkey(*args) for args in matches)
if __name__ == "__main__":
# test answer on example data
test_data, test_answer1 = ReadExamplePuzzle()
test_data = preprocess_data(test_data)
data = preprocess_data(ReadPuzzle())
for v in test_data:
print(v)
if Test(1, part1(copy(test_data)), test_answer1):
Answer(1, part1(copy(data)))
if Test(2, part2(copy(test_data)), 2713310158):
Answer(2, part2(copy(data)))