p2
This commit is contained in:
85
11/k.py
85
11/k.py
@@ -6,20 +6,20 @@ input = open(os.path.dirname(__file__) +
|
||||
|
||||
class Operation:
|
||||
def __init__(self, operand, y: int):
|
||||
self.operand = operand
|
||||
self.y = y
|
||||
self.operand: str = operand
|
||||
self.y: int = y
|
||||
|
||||
def operate(self, input: int) -> int:
|
||||
i = int(input)
|
||||
j = self.y
|
||||
j = 0
|
||||
if self.y == 'old':
|
||||
j = i
|
||||
result: int = 0
|
||||
else:
|
||||
j = int(self.y)
|
||||
if (self.operand == '+'):
|
||||
result = int(i) + int(j)
|
||||
return i + j
|
||||
if (self.operand == '*'):
|
||||
result = int(i) * int(j)
|
||||
return result
|
||||
return i * j
|
||||
|
||||
|
||||
class Monkey:
|
||||
@@ -33,36 +33,25 @@ class Monkey:
|
||||
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)
|
||||
def inspect(self, worryDivider: int, p2: bool = False):
|
||||
self.inspections += 1
|
||||
item = self.items.pop(0)
|
||||
worry = int(self.operation.operate(item))
|
||||
worry = worry//3
|
||||
item: int = self.items.pop(0)
|
||||
worryScore = self.operation.operate(item)//worryDivider
|
||||
|
||||
if worry % int(self.testNumber) == 0:
|
||||
return (self.actionTrue, worry)
|
||||
if (p2):
|
||||
worryScore %= lcm
|
||||
|
||||
if worryScore % self.testNumber == 0:
|
||||
return (self.actionTrue, worryScore)
|
||||
else:
|
||||
return (self.actionFalse, worry)
|
||||
|
||||
def __getitem__(self, i):
|
||||
return self
|
||||
|
||||
def itemCount(self):
|
||||
return len(self.items)
|
||||
return (self.actionFalse, worryScore)
|
||||
|
||||
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 = {}
|
||||
|
||||
|
||||
for l in input:
|
||||
data = l.splitlines()
|
||||
|
||||
@@ -76,17 +65,35 @@ for l in input:
|
||||
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 = []
|
||||
monkeys[int(monkeyid)] = m
|
||||
|
||||
lcm = 1
|
||||
divs = []
|
||||
for m in monkeys:
|
||||
tot.append(monkeys[m].inspections)
|
||||
divs.append(monkeys[m].testNumber)
|
||||
for x in divs:
|
||||
lcm = lcm*x
|
||||
|
||||
tot.sort()
|
||||
print(tot[-1]*tot[-2])
|
||||
|
||||
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:
|
||||
while len(monkeys[m].items) != 0:
|
||||
(nextmonkey, item) = monkeys[m].inspect(worryDivider, p2)
|
||||
monkeys[nextmonkey].items.append(item)
|
||||
tot = []
|
||||
|
||||
for m in monkeys:
|
||||
tot.append(monkeys[m].inspections)
|
||||
|
||||
tot.sort()
|
||||
return tot[-1]*tot[-2]
|
||||
|
||||
|
||||
print(process(20, 3))
|
||||
print(process(10000, 1, True))
|
||||
|
||||
Reference in New Issue
Block a user