day 11 part 1
This commit is contained in:
92
11/k.py
Normal file
92
11/k.py
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
input = open(os.path.dirname(__file__) +
|
||||||
|
"/input.txt", "r").read().split('\n\n')
|
||||||
|
|
||||||
|
|
||||||
|
class Operation:
|
||||||
|
def __init__(self, operand, y: int):
|
||||||
|
self.operand = operand
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
def operate(self, input: int) -> int:
|
||||||
|
i = int(input)
|
||||||
|
j = self.y
|
||||||
|
if self.y == 'old':
|
||||||
|
j = i
|
||||||
|
result: int = 0
|
||||||
|
if (self.operand == '+'):
|
||||||
|
result = int(i) + int(j)
|
||||||
|
if (self.operand == '*'):
|
||||||
|
result = int(i) * int(j)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
class Monkey:
|
||||||
|
def __init__(self, monkeyid: int, items, operation, testNumber: int, actionTrue: int, actionFalse: int):
|
||||||
|
self.monkeyid = monkeyid
|
||||||
|
self.items = items
|
||||||
|
op = operation.split()
|
||||||
|
self.operation = Operation(op[1], op[2])
|
||||||
|
self.testNumber = testNumber
|
||||||
|
self.actionTrue = int(actionTrue)
|
||||||
|
self.actionFalse = int(actionFalse)
|
||||||
|
self.inspections = 0
|
||||||
|
|
||||||
|
def addItem(self, item: int):
|
||||||
|
|
||||||
|
self.items.append(item)
|
||||||
|
|
||||||
|
def inspect(self):
|
||||||
|
if len(self.items) == 0:
|
||||||
|
return (-1, -1)
|
||||||
|
self.inspections += 1
|
||||||
|
item = self.items.pop(0)
|
||||||
|
worry = int(self.operation.operate(item))
|
||||||
|
worry = worry//3
|
||||||
|
|
||||||
|
if worry % int(self.testNumber) == 0:
|
||||||
|
return (self.actionTrue, worry)
|
||||||
|
else:
|
||||||
|
return (self.actionFalse, worry)
|
||||||
|
|
||||||
|
def __getitem__(self, i):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def itemCount(self):
|
||||||
|
return len(self.items)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Monkey " + str(self.monkeyid) + ": "+str(self.items)+" true: "+str(self.actionTrue)+" false: "+str(self.actionFalse)
|
||||||
|
|
||||||
|
|
||||||
|
monkeys = {}
|
||||||
|
|
||||||
|
|
||||||
|
for l in input:
|
||||||
|
data = l.splitlines()
|
||||||
|
|
||||||
|
monkeyid = data[0].split()[1][0]
|
||||||
|
items = []
|
||||||
|
itemline = data[1].strip().split(': ')
|
||||||
|
for i in itemline[1].strip().split(', '):
|
||||||
|
items.append(int(i))
|
||||||
|
operation = data[2].split('=')[1]
|
||||||
|
test = int(data[3].split()[3])
|
||||||
|
optrue = int(data[4].strip().split()[5])
|
||||||
|
opfalse = int(data[5].strip().split()[5])
|
||||||
|
m = Monkey(monkeyid, items, operation, test, optrue, opfalse)
|
||||||
|
monkeys[monkeyid] = m
|
||||||
|
|
||||||
|
for round in range(20):
|
||||||
|
for m in monkeys:
|
||||||
|
while len(monkeys[m].items) != 0:
|
||||||
|
(nextmonkey, item) = monkeys[m].inspect()
|
||||||
|
monkeys[str(nextmonkey)].addItem(int(item))
|
||||||
|
tot = []
|
||||||
|
|
||||||
|
for m in monkeys:
|
||||||
|
tot.append(monkeys[m].inspections)
|
||||||
|
|
||||||
|
tot.sort()
|
||||||
|
print(tot[-1]*tot[-2])
|
||||||
Reference in New Issue
Block a user