From eefab8eabf7da584e4500846f50d01fb54690b5b Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sat, 10 Dec 2022 21:32:55 -0800 Subject: [PATCH] day 11 python --- day11/input | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ day11/sample | 27 ++++++++++++++++++++++++++ day11/sol.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 day11/input create mode 100644 day11/sample create mode 100644 day11/sol.py diff --git a/day11/input b/day11/input new file mode 100644 index 0000000..e1c1c98 --- /dev/null +++ b/day11/input @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 93, 54, 69, 66, 71 + Operation: new = old * 3 + Test: divisible by 7 + If true: throw to monkey 7 + If false: throw to monkey 1 + +Monkey 1: + Starting items: 89, 51, 80, 66 + Operation: new = old * 17 + Test: divisible by 19 + If true: throw to monkey 5 + If false: throw to monkey 7 + +Monkey 2: + Starting items: 90, 92, 63, 91, 96, 63, 64 + Operation: new = old + 1 + Test: divisible by 13 + If true: throw to monkey 4 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 65, 77 + Operation: new = old + 2 + Test: divisible by 3 + If true: throw to monkey 4 + If false: throw to monkey 6 + +Monkey 4: + Starting items: 76, 68, 94 + Operation: new = old * old + Test: divisible by 2 + If true: throw to monkey 0 + If false: throw to monkey 6 + +Monkey 5: + Starting items: 86, 65, 66, 97, 73, 83 + Operation: new = old + 8 + Test: divisible by 11 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 6: + Starting items: 78 + Operation: new = old + 6 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 1 + +Monkey 7: + Starting items: 89, 57, 59, 61, 87, 55, 55, 88 + Operation: new = old + 7 + Test: divisible by 5 + If true: throw to monkey 2 + If false: throw to monkey 5 diff --git a/day11/sample b/day11/sample new file mode 100644 index 0000000..30e09e5 --- /dev/null +++ b/day11/sample @@ -0,0 +1,27 @@ +Monkey 0: + Starting items: 79, 98 + Operation: new = old * 19 + Test: divisible by 23 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 54, 65, 75, 74 + Operation: new = old + 6 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 79, 60, 97 + Operation: new = old * old + Test: divisible by 13 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 74 + Operation: new = old + 3 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 1 diff --git a/day11/sol.py b/day11/sol.py new file mode 100644 index 0000000..b0be8f0 --- /dev/null +++ b/day11/sol.py @@ -0,0 +1,54 @@ +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)