Skip to content

Commit

Permalink
2022-11
Browse files Browse the repository at this point in the history
  • Loading branch information
viliampucik committed Dec 12, 2022
1 parent bd6eae7 commit 25736e6
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions 2022/11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python
from collections import deque
from math import gcd, prod
from operator import floordiv, mod

ITEMS, OPER, TEST, A, B = range(5)


def solve(monkeys, iterations, op, number):
inspections = [0] * len(monkeys)

for _ in range(iterations):
for i, m in enumerate(monkeys):
while m[ITEMS]:
inspections[i] += 1
item = op(m[OPER](m[ITEMS].popleft()), number)
if item % m[TEST] == 0:
monkeys[m[A]][ITEMS].append(item)
else:
monkeys[m[B]][ITEMS].append(item)

return prod(sorted(inspections)[-2:])


monkeys = [
[
deque(map(int, item[ITEMS].replace(",", "").split()[2:])),
eval("lambda old: " + item[OPER].split(" = ")[-1]),
int(item[TEST].split()[-1]),
int(item[A].split()[-1]),
int(item[B].split()[-1]),
]
for block in open(0).read().split("\n\n")
for item in [block.splitlines()[1:]]
]

print(solve(monkeys, 20, floordiv, 3))
tests = [m[TEST] for m in monkeys]
print(solve(monkeys, 10_000, mod, prod(tests) // gcd(*tests))) # Use least common multiple

0 comments on commit 25736e6

Please sign in to comment.