diff --git a/day11/sol.py b/day11/sol.py index e9a4fdc..52f348c 100644 --- a/day11/sol.py +++ b/day11/sol.py @@ -1,4 +1,5 @@ from collections import Counter, namedtuple +from copy import deepcopy import math Monkey = namedtuple('Monkey', 'index, op, divisor, target') @@ -20,14 +21,14 @@ def parse(input): int(lines[5].split()[-1]), int(lines[4].split()[-1]), ] - monkeys.append(Monkey(items, op, divisor, target)) + monkeys.append(Monkey(i, op, divisor, target)) return monkeys, items def play(monkeys, items, rounds=1, N=None): throws = Counter() for _ in range(rounds): - for i, m in enumerate(monkeys): - for j, x in enumerate(items[i]): + for m in monkeys: + for x in items[m.index]: if N: nx = m.op(x) % N else: @@ -35,8 +36,9 @@ def play(monkeys, items, rounds=1, N=None): trg = m.target[nx % m.divisor == 0] #print(f'{i}: {x} -> {nx}. throwing to {trg}') items[trg].append(nx) - throws[i] += len(items[i]) - items[i] = [] + throws[m.index] += len(items[m.index]) + items[m.index] = [] + show(items) return throws def show(items): @@ -58,14 +60,11 @@ def main(): #print(monkeys) show(items) - tmp = {i: list(xs) for i, xs in items.items()} - throws = play(monkeys, tmp, rounds=20) - show(tmp) + throws = play(monkeys, deepcopy(items), rounds=20) print(monkeybusiness(throws)) N = lcm(m.divisor for m in monkeys) - throws = play(monkeys, items, rounds=10000, N=N) - show(items) + throws = play(monkeys, deepcopy(items), rounds=10000, N=N) print(monkeybusiness(throws)) main()