Unable to load image

ADVENT OF CODE DAY 11: MONKEY BUSINESS :marseymonke:

Those FRICKING monkeys stole your stuff! Get it back! :soycry: :!marseymonke:

(Also, remember to leave a comment if you did something interesting :marseylove:)

35
Jump in the discussion.

No email address required.

Got stuck on part 2 because of big numbers and had to get help :marseygiveup:

Added gcd after getting help

with open('./input11.txt') as infile:
    text = infile.read()

class Monkey:
    def __init__(self, op, test:int, true:int, false:int) -> None:
        self.op = op
        self.test = test
        self.true = true
        self.false = false
        self.items = []
        self.items_checked = 0

    def inspect(self):
        for item in self.items:
            self.items_checked += 1
            item = self.op(item)
            item = item % gcd
            if item % self.test == 0:
                monkeys[self.true].items.append(item)
            else:
                monkeys[self.false].items.append(item)
        self.items = []

monkeys = []

def parse_op(op):
    _, out = map(str.strip, op.split('='))
    func  = "lambda old:" + out
    return eval(func)

gcd = 1

for m in text.split('\n\n'):
    _, items, op, test, true, false = m.splitlines()
    op = parse_op(op)
    test = int(test.split()[-1])
    true = int(true.split()[-1])
    false = int(false.split()[-1])
    monkey = Monkey(op, test, true, false)
    _, items = items.split(':')
    items = map(int, items.strip().split(', '))
    monkey.items.extend(items)
    monkeys.append(monkey)
    gcd *= test

for i in range(10000):
    for m in monkeys:
        m.inspect()

a , b = sorted([m.items_checked for m in monkeys])[-2:]
print(a*b)
Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.