-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11_monkeys_pt1.py
134 lines (104 loc) · 4.55 KB
/
11_monkeys_pt1.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
import re
import math
class Monkey:
def __init__(self):
self.items = []
self.inspection_operator = '+'
self.inspection_value = 0
self.test_divisor = 1
self.true_monkey = 0
self.false_monkey = 0
self.inspections = 0
def __str__(self):
return f"{self.items}, {self.inspection_operator}, {self.inspection_value}, {self.test_divisor}, {self.true_monkey}, {self.false_monkey}"
def setStartingItems(self, new_starting_items):
self.items = new_starting_items
def addItem(self, new_item):
self.items.append(new_item)
def setInspectionValues(self, new_operator, new_value):
self.inspection_operator = new_operator
self.inspection_value = new_value
def setTestDivisor(self, new_test_divisor):
self.test_divisor = new_test_divisor
def getTestDivisor(self):
return int(self.test_divisor)
def setTrueMonkey(self, new_true_monkey):
self.true_monkey = new_true_monkey
def getTrueMonkey(self):
return self.true_monkey
def getFalseMonkey(self):
return self.false_monkey
def setFalseMonkey(self, new_false_monkey):
self.false_monkey = new_false_monkey
def getInspections(self):
return self.inspections
def inspectItem(self, i):
if self.inspection_operator == "*":
if self.inspection_value == "old":
self.items[i] = int(self.items[i]) * int(self.items[i])
else:
self.items[i] = int(self.items[i]) * int(self.inspection_value)
elif self.inspection_operator == "+":
if self.inspection_value == "old":
self.items[i] = int(self.items[i]) + int(self.items[i])
else:
self.items[i] = int(self.items[i]) + int(self.inspection_value)
self.items[i] = math.floor(self.items[i]/3)
self.inspections += 1
def throwItem(self, i):
if self.items[i] % int(self.test_divisor) == 0:
monkeys[int(self.true_monkey)].addItem(self.items.pop(i))
else:
monkeys[int(self.false_monkey)].addItem(self.items.pop(i))
def turn(self):
for i in range(len(self.items)):
# since throwing an item removes it, we'll always be dealing with the first item in the list
self.inspectItem(0)
self.throwItem(0)
def getDivisorProduct():
return_value = 1
for i in range(len(monkeys)):
return_value = return_value * monkeys[i].getTestDivisor()
return return_value
def round():
for i in range(len(monkeys)):
monkeys[i].turn()
def setupMonkeysFromInput(filename):
input = open(filename).readlines()
current_monkey = Monkey()
for line in input:
if re.search("(Monkey)", line):
current_monkey = Monkey()
# can have any amount of integers representing the starting items
if re.search("(Starting items:)", line):
current_monkey.setStartingItems((re.findall('\d+', line)))
# fifth item in the split string is the operator
# sixth item in the split string is either 'old' or a value to add/multiply
# (0-based indexing)
elif re.search("(Operation:)", line):
current_monkey.setInspectionValues(line.split()[4], line.split()[5])
# fourth item in the split string is the value to divide by for the test
elif re.search("(Test:)", line):
current_monkey.setTestDivisor(line.split()[3])
# sixth item in the split string is the index of the monkey to throw to
elif re.search("(If true:)", line):
current_monkey.setTrueMonkey(line.split()[5])
elif re.search("(If false:)", line):
current_monkey.setFalseMonkey(line.split()[5])
# If false is the last line of info for a monkey
monkeys.append(current_monkey)
monkeys = []
setupMonkeysFromInput("11_monkeys.txt")
for i in range(20):
round()
most_active_monkeys = [0 for i in range(2)]
for i in range(len(monkeys)):
for j in range( len(most_active_monkeys)):
if monkeys[i].getInspections() > most_active_monkeys[j]:
most_active_monkeys.insert(j, monkeys[i].getInspections())
most_active_monkeys.pop()
break
monkey_business = most_active_monkeys[0]
for i in range(1, len(most_active_monkeys)):
monkey_business *= most_active_monkeys[i]
print(monkey_business)