From 9f3ab834b8922d55738e2b8ae1ad04a9c3b410e1 Mon Sep 17 00:00:00 2001 From: Fedor Lyanguzov Date: Sun, 11 Dec 2022 16:58:12 +0300 Subject: [PATCH] 11 --- 11/11.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11/input | 55 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 11/11.py create mode 100644 11/input diff --git a/11/11.py b/11/11.py new file mode 100644 index 0000000..68e3cf5 --- /dev/null +++ b/11/11.py @@ -0,0 +1,77 @@ +from math import prod + +def make_monkeys(): + monkeys = { + 0: [ + [63, 84, 80, 83, 84, 53, 88, 72], + lambda x: x*11, + lambda x: x%13==0, + 4, 7], + 1: [ + [67, 56, 92, 88, 84], + lambda x: x+4, + lambda x: x%11==0, + 5, 3], + 2: [ + [52], + lambda x: x*x, + lambda x: x%2==0, + 3, 1], + 3: [ + [59, 53, 60, 92, 69, 72], + lambda x: x+2, + lambda x: x%5==0, + 5, 6], + 4: [ + [61, 52, 55, 61], + lambda x: x+3, + lambda x: x%7==0, + 7, 2], + 5: [ + [79, 53], + lambda x: x+1, + lambda x: x%3==0, + 0, 6], + 6: [ + [59, 86, 67, 95, 92, 77, 91], + lambda x: x+5, + lambda x: x%19==0, + 4, 0], + 7: [ + [58, 83, 89], + lambda x: x*19, + lambda x: x%17==0, + 2, 1], + } + return monkeys + +def reduce_worry(item): + return item//3 + +def turn(monkey, i, monkey_business, reduce_worry=lambda x: x): + items, inspect, test, left, right = monkey + while items: + item = items.pop(0) + item = inspect(item) + monkey_business[i] += 1 + item = reduce_worry(item) + if test(item): + monkeys[left][0].append(item) + else: + monkeys[right][0].append(item) + +def make_turns(n, reduce_worry=lambda x: x): + global monkeys + monkeys = make_monkeys() + monkey_business = {i:0 for i in range(8)} + for _ in range(n): + for i in range(8): + turn(monkeys[i], i, monkey_business, reduce_worry) + return prod(sorted(monkey_business.values())[-2:]) + +print(make_turns(20, reduce_worry)) + +mods = [13, 11, 2, 5, 7, 3, 19, 17] +M = prod(mods) + +print(make_turns(10_000, lambda x: x%M)) diff --git a/11/input b/11/input new file mode 100644 index 0000000..a9850e8 --- /dev/null +++ b/11/input @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 63, 84, 80, 83, 84, 53, 88, 72 + Operation: new = old * 11 + Test: divisible by 13 + If true: throw to monkey 4 + If false: throw to monkey 7 + +Monkey 1: + Starting items: 67, 56, 92, 88, 84 + Operation: new = old + 4 + Test: divisible by 11 + If true: throw to monkey 5 + If false: throw to monkey 3 + +Monkey 2: + Starting items: 52 + Operation: new = old * old + Test: divisible by 2 + If true: throw to monkey 3 + If false: throw to monkey 1 + +Monkey 3: + Starting items: 59, 53, 60, 92, 69, 72 + Operation: new = old + 2 + Test: divisible by 5 + If true: throw to monkey 5 + If false: throw to monkey 6 + +Monkey 4: + Starting items: 61, 52, 55, 61 + Operation: new = old + 3 + Test: divisible by 7 + If true: throw to monkey 7 + If false: throw to monkey 2 + +Monkey 5: + Starting items: 79, 53 + Operation: new = old + 1 + Test: divisible by 3 + If true: throw to monkey 0 + If false: throw to monkey 6 + +Monkey 6: + Starting items: 59, 86, 67, 95, 92, 77, 91 + Operation: new = old + 5 + Test: divisible by 19 + If true: throw to monkey 4 + If false: throw to monkey 0 + +Monkey 7: + Starting items: 58, 83, 89 + Operation: new = old * 19 + Test: divisible by 17 + If true: throw to monkey 2 + If false: throw to monkey 1