You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.4 KiB
85 lines
2.4 KiB
2 years ago
|
import math
|
||
|
|
||
|
class Monkey:
|
||
|
def __init__(self, items, inspect, divider, _true, _false):
|
||
|
self.inspected = 0
|
||
|
self.items = []
|
||
|
for x in items:
|
||
|
self.items.append(int(x))
|
||
|
self.divider = int(divider)
|
||
|
self._true = int(_true)
|
||
|
self._false = int(_false)
|
||
|
if '+' in inspect:
|
||
|
self.inspect_kind = 1
|
||
|
tempa, tempb = inspect.split('+ ')
|
||
|
self.inspect_amount = int(tempb)
|
||
|
elif inspect.endswith('old'):
|
||
|
self.inspect_kind = 3
|
||
|
else:
|
||
|
self.inspect_kind = 2
|
||
|
tempa, tempb = inspect.split('* ')
|
||
|
self.inspect_amount = int(tempb)
|
||
|
|
||
|
def inspect(self):
|
||
|
if self.items == []:
|
||
|
return
|
||
|
for x in range(len(self.items)):
|
||
|
self.inspected += 1
|
||
|
if self.inspect_kind == 1:
|
||
|
self.items[x] += self.inspect_amount
|
||
|
if self.inspect_kind == 2:
|
||
|
self.items[x] = self.items[x] * self.inspect_amount
|
||
|
if self.inspect_kind == 3:
|
||
|
self.items[x] = self.items[x] * self.items[x]
|
||
|
self.items[x] %= modulo
|
||
|
if self.items[x] % self.divider == 0:
|
||
|
monkeys[self._true].addItem(self.items[x])
|
||
|
else:
|
||
|
monkeys[self._false].addItem(self.items[x])
|
||
|
self.items = []
|
||
|
|
||
|
def addItem(self, item):
|
||
|
self.items.append(item)
|
||
|
|
||
|
def retInspect(self):
|
||
|
return self.inspected
|
||
|
|
||
|
|
||
|
|
||
|
with open('input11.txt','r') as f:
|
||
|
inp = f.read().splitlines(keepends=False)
|
||
|
|
||
|
monkeys = []
|
||
|
tempMain = []
|
||
|
monkeyBuisness = []
|
||
|
dividers = []
|
||
|
modulo = 1
|
||
|
|
||
|
for i in range(len(inp)):
|
||
|
tempMain.append(inp[i])
|
||
|
if inp[i] == '' or i == (len(inp) - 1):
|
||
|
tempa, tempb = tempMain[1].split(': ')
|
||
|
items = tempb.split(', ')
|
||
|
tempa, divider = tempMain[3].split('by ')
|
||
|
dividers.append(int(divider))
|
||
|
tempa, inspect = tempMain[2].split('= ')
|
||
|
tempa, _true = tempMain[4].split('monkey ')
|
||
|
tempa, _false = tempMain[5].split('monkey ')
|
||
|
newMonkey = Monkey(items, inspect, divider, _true, _false)
|
||
|
monkeys.append(newMonkey)
|
||
|
tempMain = []
|
||
|
|
||
|
|
||
|
for i in dividers:
|
||
|
modulo = modulo * i
|
||
|
|
||
|
for _ in range(10000):
|
||
|
for i in monkeys:
|
||
|
i.inspect()
|
||
|
|
||
|
for i in monkeys:
|
||
|
monkeyBuisness.append(i.retInspect())
|
||
|
|
||
|
monkeyBuisness.sort()
|
||
|
|
||
|
print(monkeyBuisness[-1]*monkeyBuisness[-2])
|