Since 2010

Advent of Code 2022 day 11

Part one solution for Advent of Code 2022 day 11. The problem description made a bit of a fuss about the exact order things happen in, so I’ve over-engineered this a bit. Couldn’t make up my mind whether to create a class for items or not either.

But part two? Nah forget it. Those are the puzzles that turn me off. Some sort of mathematics is required, and I don’t have enough knowledge to even know what knowledge I don’t have. I don’t have the inclination to find out either - maths has never interested me.

import math
import re
import utils

class Monkey:
    def __init__(self, starting_items, operation, test, true, false):
        self.items = self.parse_starting_items(starting_items)
        self.operation = operation
        self.test = int(test)
        self.true = int(true)
        self.false = int(false)
        self.items_inspected = 0

    def parse_starting_items(self, starting_items):
        return [int(n) for n in starting_items.split(", ")]

    def inspect_items(self):
        self.items = [
            eval(f"{self.operation.replace('old','item')}") for item in self.items
        self.items = [int(item / 3) for item in self.items]
        self.items_inspected += len(self.items)

    def throw_items(self):
        items = [
                "worry_level": item,
                "throw_to": self.true if bool(item % self.test == 0) else self.false,
            for item in self.items
        self.items = []
        return items

if __name__ == "__main__":
    raw_input = utils.get_raw_input_as_str("day_11_input.txt")
    monkeys = {
        int(m["monkey"]): Monkey(
            *"starting_items", "operation", "test", "true", "false")
        for m in re.finditer(
            r"^.*(?P<monkey>\d+):\n.*: (?P<starting_items>.*)\n.*= (?P<operation>.*)\n.* (?P<test>\d+)\n.*(?P<true>\d+)\n.*(?P<false>\d+)",

    for _ in range(20):
        for k, monkey in monkeys.items():
            for item in monkey.throw_items():

    for k, monkey in monkeys.items():
        print(f"{k=} {monkey.items=} {monkey.items_inspected=}")

    print([monkey.items_inspected for monkey in monkeys.values()])[-2:])