diff options
| author | 2022-12-11 16:45:04 +0100 | |
|---|---|---|
| committer | 2022-12-11 16:45:04 +0100 | |
| commit | e35d13c79571290242e8cc82023b0fef4f6e4919 (patch) | |
| tree | 21558e3c39a3ec8eda12e7605aaafeb0c8094dd7 /day11/__init__.py | |
| parent | 9d22965e3daa2608af6bd98799a04b4cdab88abd (diff) | |
| download | 2022-e35d13c79571290242e8cc82023b0fef4f6e4919.tar.gz 2022-e35d13c79571290242e8cc82023b0fef4f6e4919.tar.bz2 2022-e35d13c79571290242e8cc82023b0fef4f6e4919.zip | |
Day 11
Diffstat (limited to 'day11/__init__.py')
| -rw-r--r-- | day11/__init__.py | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/day11/__init__.py b/day11/__init__.py index 7ed8fbc..f799afb 100644 --- a/day11/__init__.py +++ b/day11/__init__.py | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | import dataclasses | 2 | import dataclasses |
| 3 | from abc import ABC | 3 | from abc import ABC |
| 4 | from functools import reduce | ||
| 4 | from typing import List, Iterator, Any, Tuple | 5 | from typing import List, Iterator, Any, Tuple |
| 5 | 6 | ||
| 6 | from aoc import BaseAssignment | 7 | from aoc import BaseAssignment |
| @@ -13,12 +14,11 @@ class Monkey: | |||
| 13 | test_divisible: int | 14 | test_divisible: int |
| 14 | test_true: int | 15 | test_true: int |
| 15 | test_false: int | 16 | test_false: int |
| 16 | worry_divider: int = 1 | ||
| 17 | 17 | ||
| 18 | inspections: int = 0 | 18 | inspections: int = 0 |
| 19 | 19 | ||
| 20 | @classmethod | 20 | @classmethod |
| 21 | def from_input(cls, lines: List[str], worry_divider: int): | 21 | def from_input(cls, lines: List[str]): |
| 22 | _, items = lines[1].split(":") | 22 | _, items = lines[1].split(":") |
| 23 | 23 | ||
| 24 | return cls( | 24 | return cls( |
| @@ -27,7 +27,6 @@ class Monkey: | |||
| 27 | test_divisible=int(lines[3].split(" ")[-1]), | 27 | test_divisible=int(lines[3].split(" ")[-1]), |
| 28 | test_true=int(lines[4].split(" ")[-1]), | 28 | test_true=int(lines[4].split(" ")[-1]), |
| 29 | test_false=int(lines[5].split(" ")[-1]), | 29 | test_false=int(lines[5].split(" ")[-1]), |
| 30 | worry_divider=worry_divider, | ||
| 31 | ) | 30 | ) |
| 32 | 31 | ||
| 33 | def new_worry_factor(self, item: int, worry_factor=1) -> int: | 32 | def new_worry_factor(self, item: int, worry_factor=1) -> int: |
| @@ -45,41 +44,46 @@ class Monkey: | |||
| 45 | return self.test_true | 44 | return self.test_true |
| 46 | return self.test_false | 45 | return self.test_false |
| 47 | 46 | ||
| 48 | def inspect_items(self) -> Iterator[Tuple[int, int]]: | 47 | def inspect_items(self, worry_divisor: int) -> Iterator[Tuple[int, int]]: |
| 49 | for item in self.items: | 48 | for item in self.items: |
| 50 | self.inspections += 1 | 49 | self.inspections += 1 |
| 51 | new_worry_factor = self.new_worry_factor(item) // self.worry_divider | 50 | new_worry_factor = self.new_worry_factor(item) // worry_divisor |
| 52 | yield new_worry_factor, self.throw_to(new_worry_factor) | 51 | yield new_worry_factor, self.throw_to(new_worry_factor) |
| 53 | 52 | ||
| 54 | self.items = [] | 53 | self.items = [] |
| 55 | 54 | ||
| 56 | 55 | ||
| 57 | class Assignment(BaseAssignment, ABC): | 56 | class Assignment(BaseAssignment, ABC): |
| 58 | def parse_monkeys(self, input: Iterator[str], worry_divider: int): | 57 | def parse_monkeys(self, input: Iterator[str]): |
| 59 | monkeys = [] | 58 | monkeys = [] |
| 60 | monkey = [] | 59 | monkey = [] |
| 61 | for line in input: | 60 | for line in input: |
| 62 | if line == "": | 61 | if line == "": |
| 63 | monkeys.append(Monkey.from_input(monkey, worry_divider)) | 62 | monkeys.append(Monkey.from_input(monkey)) |
| 64 | monkey = [] | 63 | monkey = [] |
| 65 | continue | 64 | continue |
| 66 | 65 | ||
| 67 | monkey.append(line) | 66 | monkey.append(line) |
| 68 | 67 | ||
| 69 | if len(monkey) != 0: | 68 | if len(monkey) != 0: |
| 70 | monkeys.append(Monkey.from_input(monkey, worry_divider)) | 69 | monkeys.append(Monkey.from_input(monkey)) |
| 71 | 70 | ||
| 72 | return monkeys | 71 | return monkeys |
| 73 | 72 | ||
| 74 | def calculate_monkey_business( | 73 | def calculate_monkey_business( |
| 75 | self, input: Iterator[str], rounds: int, worry_divider: int = 1 | 74 | self, |
| 75 | monkeys: list[Monkey], | ||
| 76 | rounds: int, | ||
| 77 | worry_divisor: int = 1, | ||
| 78 | common_devisor: int = None, | ||
| 76 | ): | 79 | ): |
| 77 | monkeys = self.parse_monkeys(input, worry_divider) | ||
| 78 | 80 | ||
| 79 | for _ in range(rounds): | 81 | for _ in range(rounds): |
| 80 | for monkey in monkeys: | 82 | for monkey in monkeys: |
| 81 | for item, other_monkey in monkey.inspect_items(): | 83 | for item, other_monkey in monkey.inspect_items(worry_divisor): |
| 82 | monkeys[other_monkey].items.append(item) | 84 | monkeys[other_monkey].items.append( |
| 85 | item % common_devisor if common_devisor is not None else item | ||
| 86 | ) | ||
| 83 | 87 | ||
| 84 | a, b = sorted([monkey.inspections for monkey in monkeys])[-2:] | 88 | a, b = sorted([monkey.inspections for monkey in monkeys])[-2:] |
| 85 | 89 | ||
| @@ -90,11 +94,16 @@ class AssignmentOne(Assignment): | |||
| 90 | example_result = 10605 | 94 | example_result = 10605 |
| 91 | 95 | ||
| 92 | def run(self, input: Iterator) -> Any: | 96 | def run(self, input: Iterator) -> Any: |
| 93 | return self.calculate_monkey_business(input, 20, 3) | 97 | monkeys = self.parse_monkeys(input) |
| 98 | return self.calculate_monkey_business(monkeys, 20, worry_divisor=3) | ||
| 94 | 99 | ||
| 95 | 100 | ||
| 96 | class AssignmentTwo(Assignment): | 101 | class AssignmentTwo(Assignment): |
| 97 | example_result = 2713310158 | 102 | example_result = 2713310158 |
| 98 | 103 | ||
| 99 | def run(self, input: Iterator) -> Any: | 104 | def run(self, input: Iterator) -> Any: |
| 100 | return self.calculate_monkey_business(input, 10000) | 105 | monkeys = self.parse_monkeys(input) |
| 106 | common_divisor = reduce(lambda c, m: c * m.test_divisible, monkeys, 1) | ||
| 107 | return self.calculate_monkey_business( | ||
| 108 | monkeys, 10000, common_devisor=common_divisor | ||
| 109 | ) | ||
