#!/usr/bin/env python3 lines = (x.strip() for x in open("input.txt")) monkeys = [] def create_op(op_line): if "old * old" in op_line: return lambda x: x**2 val = int(op_line[6:]) return (lambda x: x*val) if "*" in op_line else (lambda x: x+val) for line in lines: monkeys.append(( [*map(int, next(lines)[16:].split(", "))], create_op(next(lines)[17:]), int(next(lines)[19:]), int(next(lines)[25:]), int(next(lines)[26:]), [0] )) next(lines, None) for _ in range(20): for items, op, div, if_true, if_false, count in monkeys: while items: count[0] += 1 worry = op(items.pop(0)) // 3 monkeys[if_true if worry % div == 0 else if_false][0].append(worry) insp = sorted(monkey[-1][0] for monkey in monkeys) print(insp[-2]*insp[-1])