From e35d13c79571290242e8cc82023b0fef4f6e4919 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sun, 11 Dec 2022 16:45:04 +0100 Subject: Day 11 --- day11/__init__.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'day11') 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 @@ # -*- coding: utf-8 -*- import dataclasses from abc import ABC +from functools import reduce from typing import List, Iterator, Any, Tuple from aoc import BaseAssignment @@ -13,12 +14,11 @@ class Monkey: test_divisible: int test_true: int test_false: int - worry_divider: int = 1 inspections: int = 0 @classmethod - def from_input(cls, lines: List[str], worry_divider: int): + def from_input(cls, lines: List[str]): _, items = lines[1].split(":") return cls( @@ -27,7 +27,6 @@ class Monkey: test_divisible=int(lines[3].split(" ")[-1]), test_true=int(lines[4].split(" ")[-1]), test_false=int(lines[5].split(" ")[-1]), - worry_divider=worry_divider, ) def new_worry_factor(self, item: int, worry_factor=1) -> int: @@ -45,41 +44,46 @@ class Monkey: return self.test_true return self.test_false - def inspect_items(self) -> Iterator[Tuple[int, int]]: + def inspect_items(self, worry_divisor: int) -> Iterator[Tuple[int, int]]: for item in self.items: self.inspections += 1 - new_worry_factor = self.new_worry_factor(item) // self.worry_divider + new_worry_factor = self.new_worry_factor(item) // worry_divisor yield new_worry_factor, self.throw_to(new_worry_factor) self.items = [] class Assignment(BaseAssignment, ABC): - def parse_monkeys(self, input: Iterator[str], worry_divider: int): + def parse_monkeys(self, input: Iterator[str]): monkeys = [] monkey = [] for line in input: if line == "": - monkeys.append(Monkey.from_input(monkey, worry_divider)) + monkeys.append(Monkey.from_input(monkey)) monkey = [] continue monkey.append(line) if len(monkey) != 0: - monkeys.append(Monkey.from_input(monkey, worry_divider)) + monkeys.append(Monkey.from_input(monkey)) return monkeys def calculate_monkey_business( - self, input: Iterator[str], rounds: int, worry_divider: int = 1 + self, + monkeys: list[Monkey], + rounds: int, + worry_divisor: int = 1, + common_devisor: int = None, ): - monkeys = self.parse_monkeys(input, worry_divider) for _ in range(rounds): for monkey in monkeys: - for item, other_monkey in monkey.inspect_items(): - monkeys[other_monkey].items.append(item) + for item, other_monkey in monkey.inspect_items(worry_divisor): + monkeys[other_monkey].items.append( + item % common_devisor if common_devisor is not None else item + ) a, b = sorted([monkey.inspections for monkey in monkeys])[-2:] @@ -90,11 +94,16 @@ class AssignmentOne(Assignment): example_result = 10605 def run(self, input: Iterator) -> Any: - return self.calculate_monkey_business(input, 20, 3) + monkeys = self.parse_monkeys(input) + return self.calculate_monkey_business(monkeys, 20, worry_divisor=3) class AssignmentTwo(Assignment): example_result = 2713310158 def run(self, input: Iterator) -> Any: - return self.calculate_monkey_business(input, 10000) + monkeys = self.parse_monkeys(input) + common_divisor = reduce(lambda c, m: c * m.test_divisible, monkeys, 1) + return self.calculate_monkey_business( + monkeys, 10000, common_devisor=common_divisor + ) -- cgit v1.2.3