55 lines
1.4 KiB
Python
55 lines
1.4 KiB
Python
from collections import Counter
|
|
def parse(input):
|
|
if hasattr(input, 'read'):
|
|
input = input.read()
|
|
monkeys = []
|
|
for chunk in input.split("\n\n"):
|
|
lines = chunk.strip().split("\n")
|
|
assert 'Starting' in lines[1]
|
|
items = [int(x.strip()) for x in lines[1].split(":")[1].split(",")]
|
|
assert 'Operation' in lines[2]
|
|
op = eval("lambda old: " + lines[2].split("=")[1])
|
|
assert 'divisible by' in lines[3]
|
|
divisor = int(lines[3].split()[-1])
|
|
assert 'true: throw' in lines[4]
|
|
assert 'false: throw' in lines[5]
|
|
target = [
|
|
int(lines[5].split()[-1]),
|
|
int(lines[4].split()[-1]),
|
|
]
|
|
|
|
monkeys.append((items, op, divisor, target))
|
|
return monkeys
|
|
|
|
def product(it):
|
|
t = 1
|
|
for x in it:
|
|
t *= x
|
|
return t
|
|
|
|
throws = Counter()
|
|
def play(monkeys):
|
|
N = product(m[2] for m in monkeys)
|
|
for i, m in enumerate(monkeys):
|
|
items, op, divisor, target = m
|
|
for j, x in enumerate(items):
|
|
throws[i] += 1
|
|
old = x
|
|
x = op(x)
|
|
#x //= 3
|
|
x %= N
|
|
trg = target[x % divisor == 0]
|
|
#print(f'{old} -> {x}. throwing to {trg}')
|
|
monkeys[trg][0].append(x)
|
|
items[:] = []
|
|
return throws
|
|
|
|
m = parse(open("input"))
|
|
print(m)
|
|
for _ in range(10000):
|
|
play(m)
|
|
for i, m in enumerate(m):
|
|
print(i, m[0])
|
|
a, b = sorted(throws.values())[-2:]
|
|
print(a*b)
|