-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-11.py
98 lines (77 loc) · 2.85 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
class Monkey:
def __init__(self, items, operation, test):
self.items = items
self.operation = operation
self.test = test
self.numberOfInspections = 0
def takeTurn(self, anxietyReduce=True): # returns index of monkey to send item to
currentItem = int(self.items.pop(0))
if currentItem:
value = self.operation(currentItem)
if anxietyReduce:
value = int(value / 3)
dest = self.test(value)
self.numberOfInspections += 1
return [value, dest]
else:
print("This monkey isn't holding any items")
return [None, -1]
def printMonkey(self):
print(self.items)
@staticmethod
def buildMonkey(inputText):
items = []
operation = lambda x: x
test = lambda x: x
dests = []
modulo = 1
for line in inputText.splitlines():
print(line)
match line.strip()[:5]:
case "Start":
print("Here")
items = line[18:].split(", ")
case "Opera": # operation
operation = eval(f"lambda old: {line[19:]}")
case "Test:":
modulo = int(line.rsplit(" ", 1)[-1])
case "If tr" | "If fa":
dests.append(int(line.rsplit(" ", 1)[-1]))
if len(dests) == 2:
test = eval(f'lambda x: {dests[0]} if x % {modulo} == 0 else {dests[1]}')
dests = []
return Monkey(items, operation, test)
def partOne(input):
monkeys = []
for line in input:
monkeys.append(Monkey.buildMonkey(line))
for round in range(0, 20):
for monkey in monkeys:
while monkey.items:
[item, dest] = monkey.takeTurn()
monkeys[dest].items.append(item)
for monkey in monkeys:
monkey.printMonkey()
monkeys = sorted(monkeys, key=lambda x: x.numberOfInspections, reverse=True)
return monkeys[0].numberOfInspections * monkeys[1].numberOfInspections
def partTwo(input):
monkeys = []
for line in input:
monkeys.append(Monkey.buildMonkey(line))
for round in range(0, 10000):
for monkey in monkeys:
while monkey.items:
[item, dest] = monkey.takeTurn(False)
monkeys[dest].items.append(item)
for monkey in monkeys:
monkey.printMonkey()
monkeys = sorted(monkeys, key=lambda x: x.numberOfInspections, reverse=True)
return monkeys[0].numberOfInspections * monkeys[1].numberOfInspections
def main():
f = open('./input/day-11.txt', 'r')
input = f.read().split('\n\n')
print(partOne(input))
print(partTwo(input))
if __name__ == '__main__':
main()