This commit is contained in:
2022-12-11 11:39:37 +02:00
parent 081bf29b8c
commit 77ad783a6a

79
11/k.py
View File

@@ -6,20 +6,20 @@ input = open(os.path.dirname(__file__) +
class Operation: class Operation:
def __init__(self, operand, y: int): def __init__(self, operand, y: int):
self.operand = operand self.operand: str = operand
self.y = y self.y: int = y
def operate(self, input: int) -> int: def operate(self, input: int) -> int:
i = int(input) i = int(input)
j = self.y j = 0
if self.y == 'old': if self.y == 'old':
j = i j = i
result: int = 0 else:
j = int(self.y)
if (self.operand == '+'): if (self.operand == '+'):
result = int(i) + int(j) return i + j
if (self.operand == '*'): if (self.operand == '*'):
result = int(i) * int(j) return i * j
return result
class Monkey: class Monkey:
@@ -33,36 +33,25 @@ class Monkey:
self.actionFalse = int(actionFalse) self.actionFalse = int(actionFalse)
self.inspections = 0 self.inspections = 0
def addItem(self, item: int): def inspect(self, worryDivider: int, p2: bool = False):
self.items.append(item)
def inspect(self):
if len(self.items) == 0:
return (-1, -1)
self.inspections += 1 self.inspections += 1
item = self.items.pop(0) item: int = self.items.pop(0)
worry = int(self.operation.operate(item)) worryScore = self.operation.operate(item)//worryDivider
worry = worry//3
if worry % int(self.testNumber) == 0: if (p2):
return (self.actionTrue, worry) worryScore %= lcm
if worryScore % self.testNumber == 0:
return (self.actionTrue, worryScore)
else: else:
return (self.actionFalse, worry) return (self.actionFalse, worryScore)
def __getitem__(self, i):
return self
def itemCount(self):
return len(self.items)
def __str__(self): def __str__(self):
return "Monkey " + str(self.monkeyid) + ": "+str(self.items)+" true: "+str(self.actionTrue)+" false: "+str(self.actionFalse) return "Monkey " + str(self.monkeyid) + ": "+str(self.items)+" true: "+str(self.actionTrue)+" false: "+str(self.actionFalse)+" inspect: "+str(self.inspections)
monkeys = {} monkeys = {}
for l in input: for l in input:
data = l.splitlines() data = l.splitlines()
@@ -76,17 +65,35 @@ for l in input:
optrue = int(data[4].strip().split()[5]) optrue = int(data[4].strip().split()[5])
opfalse = int(data[5].strip().split()[5]) opfalse = int(data[5].strip().split()[5])
m = Monkey(monkeyid, items, operation, test, optrue, opfalse) m = Monkey(monkeyid, items, operation, test, optrue, opfalse)
monkeys[monkeyid] = m monkeys[int(monkeyid)] = m
for round in range(20): lcm = 1
divs = []
for m in monkeys:
divs.append(monkeys[m].testNumber)
for x in divs:
lcm = lcm*x
def printMonkeys():
for m in monkeys:
print(monkeys[m])
def process(rounds: int, worryDivider: int, p2: bool = False):
for round in range(1, rounds+1):
for m in monkeys: for m in monkeys:
while len(monkeys[m].items) != 0: while len(monkeys[m].items) != 0:
(nextmonkey, item) = monkeys[m].inspect() (nextmonkey, item) = monkeys[m].inspect(worryDivider, p2)
monkeys[str(nextmonkey)].addItem(int(item)) monkeys[nextmonkey].items.append(item)
tot = [] tot = []
for m in monkeys: for m in monkeys:
tot.append(monkeys[m].inspections) tot.append(monkeys[m].inspections)
tot.sort() tot.sort()
print(tot[-1]*tot[-2]) return tot[-1]*tot[-2]
print(process(20, 3))
print(process(10000, 1, True))