monkeyBusiness = ({ monkeys, rounds, relief }) => {
monkeys = _.cloneDeep(monkeys);
let modulo = monkeys.map((m) => m.divisor).reduce(aoc.lcm);
let inspectionCount = monkeys.map(() => 0);
for (let round = 0; round < rounds; round++) {
for (const monkey of monkeys) {
let items = monkey.items;
monkey.items = [];
for (let item of items) {
inspectionCount[monkey.id] += 1;
let { op, argA, argB } = monkey.op;
if (argA === "old") argA = item;
if (argB === "old") argB = item;
item = (op === "*" ? argA * argB : argA + argB) % modulo;
if (relief) item = Math.floor(item / 3);
let throwTarget =
item % monkey.divisor === 0 ? monkey.ifTrue : monkey.ifFalse;
monkeys[throwTarget].items.push(item);
}
}
}
inspectionCount = d3.sort(inspectionCount);
return inspectionCount.at(-1) * inspectionCount.at(-2);
}