This commit is contained in:
2022-12-11 12:25:07 +02:00
parent 77ad783a6a
commit e2c0fba68c

52
11/k.py
View File

@@ -1,8 +1,5 @@
import os import os
input = open(os.path.dirname(__file__) +
"/input.txt", "r").read().split('\n\n')
class Operation: class Operation:
def __init__(self, operand, y: int): def __init__(self, operand, y: int):
@@ -23,7 +20,8 @@ class Operation:
class Monkey: class Monkey:
def __init__(self, monkeyid: int, items, operation, testNumber: int, actionTrue: int, actionFalse: int): def __init__(self, monkeyid: int, items, operation, testNumber: int,
actionTrue: int, actionFalse: int):
self.monkeyid = monkeyid self.monkeyid = monkeyid
self.items = items self.items = items
op = operation.split() op = operation.split()
@@ -47,14 +45,24 @@ class Monkey:
return (self.actionFalse, worryScore) return (self.actionFalse, worryScore)
def __str__(self): def __str__(self):
return "Monkey " + str(self.monkeyid) + ": "+str(self.items)+" true: "+str(self.actionTrue)+" false: "+str(self.actionFalse)+" inspect: "+str(self.inspections) return "Monkey " + str(self.monkeyid) + ": "+str(self.items)+" true: " \
+ str(self.actionTrue)+" false: "+str(self.actionFalse) + \
" inspect: "+str(self.inspections)
monkeys = {} monkeys = []
lcm = 1
def readMonkeys():
global monkeys
monkeys.clear()
input = open(os.path.dirname(__file__) +
"/input.txt", "r").read().split('\n\n')
for l in input: for l in input:
data = l.splitlines() data = l.splitlines()
monkeyid = data[0].split()[1][0] monkeyid = data[0].split()[1][0]
items = [] items = []
itemline = data[1].strip().split(': ') itemline = data[1].strip().split(': ')
@@ -64,36 +72,30 @@ for l in input:
test = int(data[3].split()[3]) test = int(data[3].split()[3])
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) monkeys.append(
monkeys[int(monkeyid)] = m Monkey(monkeyid, items, operation, test, optrue, opfalse))
global lcm
lcm = 1 lcm = 1
divs = []
for m in monkeys: for m in monkeys:
divs.append(monkeys[m].testNumber) lcm = lcm*m.testNumber
for x in divs:
lcm = lcm*x
def printMonkeys(): def process(rounds: int, div: int, p2: bool = False):
for m in monkeys: readMonkeys()
print(monkeys[m])
def process(rounds: int, worryDivider: int, p2: bool = False):
for round in range(1, rounds+1): for round in range(1, rounds+1):
for m in monkeys: for m in monkeys:
while len(monkeys[m].items) != 0: while len(m.items) != 0:
(nextmonkey, item) = monkeys[m].inspect(worryDivider, p2) (nextmonkey, item) = m.inspect(div, p2)
monkeys[nextmonkey].items.append(item) monkeys[nextmonkey].items.append(item)
tot = [] tot = []
for m in monkeys: for m in monkeys:
tot.append(monkeys[m].inspections) tot.append(m.inspections)
tot.sort() tot.sort()
return tot[-1]*tot[-2] return tot[-1]*tot[-2]
print(process(20, 3)) print("p1:", process(20, 3))
print(process(10000, 1, True)) print("p2:", process(10000, 1, True))