Clean up
This commit is contained in:
72
11/k.py
72
11/k.py
@@ -1,8 +1,5 @@
|
||||
import os
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").read().split('\n\n')
|
||||
|
||||
|
||||
class Operation:
|
||||
def __init__(self, operand, y: int):
|
||||
@@ -23,7 +20,8 @@ class Operation:
|
||||
|
||||
|
||||
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.items = items
|
||||
op = operation.split()
|
||||
@@ -47,53 +45,57 @@ class Monkey:
|
||||
return (self.actionFalse, worryScore)
|
||||
|
||||
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 = {}
|
||||
|
||||
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[int(monkeyid)] = m
|
||||
|
||||
monkeys = []
|
||||
lcm = 1
|
||||
divs = []
|
||||
for m in monkeys:
|
||||
divs.append(monkeys[m].testNumber)
|
||||
for x in divs:
|
||||
lcm = lcm*x
|
||||
|
||||
|
||||
def printMonkeys():
|
||||
def readMonkeys():
|
||||
global monkeys
|
||||
monkeys.clear()
|
||||
|
||||
input = open(os.path.dirname(__file__) +
|
||||
"/input.txt", "r").read().split('\n\n')
|
||||
|
||||
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])
|
||||
monkeys.append(
|
||||
Monkey(monkeyid, items, operation, test, optrue, opfalse))
|
||||
global lcm
|
||||
lcm = 1
|
||||
for m in monkeys:
|
||||
print(monkeys[m])
|
||||
lcm = lcm*m.testNumber
|
||||
|
||||
|
||||
def process(rounds: int, worryDivider: int, p2: bool = False):
|
||||
def process(rounds: int, div: int, p2: bool = False):
|
||||
readMonkeys()
|
||||
for round in range(1, rounds+1):
|
||||
for m in monkeys:
|
||||
while len(monkeys[m].items) != 0:
|
||||
(nextmonkey, item) = monkeys[m].inspect(worryDivider, p2)
|
||||
while len(m.items) != 0:
|
||||
(nextmonkey, item) = m.inspect(div, p2)
|
||||
monkeys[nextmonkey].items.append(item)
|
||||
|
||||
tot = []
|
||||
|
||||
for m in monkeys:
|
||||
tot.append(monkeys[m].inspections)
|
||||
tot.append(m.inspections)
|
||||
|
||||
tot.sort()
|
||||
return tot[-1]*tot[-2]
|
||||
|
||||
|
||||
print(process(20, 3))
|
||||
print(process(10000, 1, True))
|
||||
print("p1:", process(20, 3))
|
||||
print("p2:", process(10000, 1, True))
|
||||
|
||||
Reference in New Issue
Block a user