diff options
| -rw-r--r-- | day11/__init__.py | 100 | ||||
| -rw-r--r-- | day11/example.txt | 27 | ||||
| -rw-r--r-- | day11/input.txt | 55 |
3 files changed, 182 insertions, 0 deletions
diff --git a/day11/__init__.py b/day11/__init__.py new file mode 100644 index 0000000..7ed8fbc --- /dev/null +++ b/day11/__init__.py | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | import dataclasses | ||
| 3 | from abc import ABC | ||
| 4 | from typing import List, Iterator, Any, Tuple | ||
| 5 | |||
| 6 | from aoc import BaseAssignment | ||
| 7 | |||
| 8 | |||
| 9 | @dataclasses.dataclass | ||
| 10 | class Monkey: | ||
| 11 | items: List[int] | ||
| 12 | operation: List[str] | ||
| 13 | test_divisible: int | ||
| 14 | test_true: int | ||
| 15 | test_false: int | ||
| 16 | worry_divider: int = 1 | ||
| 17 | |||
| 18 | inspections: int = 0 | ||
| 19 | |||
| 20 | @classmethod | ||
| 21 | def from_input(cls, lines: List[str], worry_divider: int): | ||
| 22 | _, items = lines[1].split(":") | ||
| 23 | |||
| 24 | return cls( | ||
| 25 | items=[int(item) for item in items.split(",")], | ||
| 26 | operation=lines[2].split(" ")[-2:], | ||
| 27 | test_divisible=int(lines[3].split(" ")[-1]), | ||
| 28 | test_true=int(lines[4].split(" ")[-1]), | ||
| 29 | test_false=int(lines[5].split(" ")[-1]), | ||
| 30 | worry_divider=worry_divider, | ||
| 31 | ) | ||
| 32 | |||
| 33 | def new_worry_factor(self, item: int, worry_factor=1) -> int: | ||
| 34 | operator, value = self.operation | ||
| 35 | value = item if value == "old" else int(value) | ||
| 36 | |||
| 37 | match operator: | ||
| 38 | case "+": | ||
| 39 | return item + value | ||
| 40 | case "*": | ||
| 41 | return item * value | ||
| 42 | |||
| 43 | def throw_to(self, new_worry_factor: int) -> int: | ||
| 44 | if new_worry_factor % self.test_divisible == 0: | ||
| 45 | return self.test_true | ||
| 46 | return self.test_false | ||
| 47 | |||
| 48 | def inspect_items(self) -> Iterator[Tuple[int, int]]: | ||
| 49 | for item in self.items: | ||
| 50 | self.inspections += 1 | ||
| 51 | new_worry_factor = self.new_worry_factor(item) // self.worry_divider | ||
| 52 | yield new_worry_factor, self.throw_to(new_worry_factor) | ||
| 53 | |||
| 54 | self.items = [] | ||
| 55 | |||
| 56 | |||
| 57 | class Assignment(BaseAssignment, ABC): | ||
| 58 | def parse_monkeys(self, input: Iterator[str], worry_divider: int): | ||
| 59 | monkeys = [] | ||
| 60 | monkey = [] | ||
| 61 | for line in input: | ||
| 62 | if line == "": | ||
| 63 | monkeys.append(Monkey.from_input(monkey, worry_divider)) | ||
| 64 | monkey = [] | ||
| 65 | continue | ||
| 66 | |||
| 67 | monkey.append(line) | ||
| 68 | |||
| 69 | if len(monkey) != 0: | ||
| 70 | monkeys.append(Monkey.from_input(monkey, worry_divider)) | ||
| 71 | |||
| 72 | return monkeys | ||
| 73 | |||
| 74 | def calculate_monkey_business( | ||
| 75 | self, input: Iterator[str], rounds: int, worry_divider: int = 1 | ||
| 76 | ): | ||
| 77 | monkeys = self.parse_monkeys(input, worry_divider) | ||
| 78 | |||
| 79 | for _ in range(rounds): | ||
| 80 | for monkey in monkeys: | ||
| 81 | for item, other_monkey in monkey.inspect_items(): | ||
| 82 | monkeys[other_monkey].items.append(item) | ||
| 83 | |||
| 84 | a, b = sorted([monkey.inspections for monkey in monkeys])[-2:] | ||
| 85 | |||
| 86 | return a * b | ||
| 87 | |||
| 88 | |||
| 89 | class AssignmentOne(Assignment): | ||
| 90 | example_result = 10605 | ||
| 91 | |||
| 92 | def run(self, input: Iterator) -> Any: | ||
| 93 | return self.calculate_monkey_business(input, 20, 3) | ||
| 94 | |||
| 95 | |||
| 96 | class AssignmentTwo(Assignment): | ||
| 97 | example_result = 2713310158 | ||
| 98 | |||
| 99 | def run(self, input: Iterator) -> Any: | ||
| 100 | return self.calculate_monkey_business(input, 10000) | ||
diff --git a/day11/example.txt b/day11/example.txt new file mode 100644 index 0000000..30e09e5 --- /dev/null +++ b/day11/example.txt | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | Monkey 0: | ||
| 2 | Starting items: 79, 98 | ||
| 3 | Operation: new = old * 19 | ||
| 4 | Test: divisible by 23 | ||
| 5 | If true: throw to monkey 2 | ||
| 6 | If false: throw to monkey 3 | ||
| 7 | |||
| 8 | Monkey 1: | ||
| 9 | Starting items: 54, 65, 75, 74 | ||
| 10 | Operation: new = old + 6 | ||
| 11 | Test: divisible by 19 | ||
| 12 | If true: throw to monkey 2 | ||
| 13 | If false: throw to monkey 0 | ||
| 14 | |||
| 15 | Monkey 2: | ||
| 16 | Starting items: 79, 60, 97 | ||
| 17 | Operation: new = old * old | ||
| 18 | Test: divisible by 13 | ||
| 19 | If true: throw to monkey 1 | ||
| 20 | If false: throw to monkey 3 | ||
| 21 | |||
| 22 | Monkey 3: | ||
| 23 | Starting items: 74 | ||
| 24 | Operation: new = old + 3 | ||
| 25 | Test: divisible by 17 | ||
| 26 | If true: throw to monkey 0 | ||
| 27 | If false: throw to monkey 1 | ||
diff --git a/day11/input.txt b/day11/input.txt new file mode 100644 index 0000000..0dd3bf4 --- /dev/null +++ b/day11/input.txt | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | Monkey 0: | ||
| 2 | Starting items: 80 | ||
| 3 | Operation: new = old * 5 | ||
| 4 | Test: divisible by 2 | ||
| 5 | If true: throw to monkey 4 | ||
| 6 | If false: throw to monkey 3 | ||
| 7 | |||
| 8 | Monkey 1: | ||
| 9 | Starting items: 75, 83, 74 | ||
| 10 | Operation: new = old + 7 | ||
| 11 | Test: divisible by 7 | ||
| 12 | If true: throw to monkey 5 | ||
| 13 | If false: throw to monkey 6 | ||
| 14 | |||
| 15 | Monkey 2: | ||
| 16 | Starting items: 86, 67, 61, 96, 52, 63, 73 | ||
| 17 | Operation: new = old + 5 | ||
| 18 | Test: divisible by 3 | ||
| 19 | If true: throw to monkey 7 | ||
| 20 | If false: throw to monkey 0 | ||
| 21 | |||
| 22 | Monkey 3: | ||
| 23 | Starting items: 85, 83, 55, 85, 57, 70, 85, 52 | ||
| 24 | Operation: new = old + 8 | ||
| 25 | Test: divisible by 17 | ||
| 26 | If true: throw to monkey 1 | ||
| 27 | If false: throw to monkey 5 | ||
| 28 | |||
| 29 | Monkey 4: | ||
| 30 | Starting items: 67, 75, 91, 72, 89 | ||
| 31 | Operation: new = old + 4 | ||
| 32 | Test: divisible by 11 | ||
| 33 | If true: throw to monkey 3 | ||
| 34 | If false: throw to monkey 1 | ||
| 35 | |||
| 36 | Monkey 5: | ||
| 37 | Starting items: 66, 64, 68, 92, 68, 77 | ||
| 38 | Operation: new = old * 2 | ||
| 39 | Test: divisible by 19 | ||
| 40 | If true: throw to monkey 6 | ||
| 41 | If false: throw to monkey 2 | ||
| 42 | |||
| 43 | Monkey 6: | ||
| 44 | Starting items: 97, 94, 79, 88 | ||
| 45 | Operation: new = old * old | ||
| 46 | Test: divisible by 5 | ||
| 47 | If true: throw to monkey 2 | ||
| 48 | If false: throw to monkey 7 | ||
| 49 | |||
| 50 | Monkey 7: | ||
| 51 | Starting items: 77, 85 | ||
| 52 | Operation: new = old + 6 | ||
| 53 | Test: divisible by 13 | ||
| 54 | If true: throw to monkey 4 | ||
| 55 | If false: throw to monkey 0 | ||
