adventofcode/2022/11/part2.js
2022-12-11 19:38:10 -05:00

35 lines
760 B
JavaScript

import * as R from "remeda";
const playTurn = (max, monkeys, monkey) => {
monkey.inspections += monkey.items.length;
monkey.items
.map((i) => monkey.operation(i) % max)
.forEach((i) => {
monkeys[i % monkey.divisible ? monkey.ifFalse : monkey.ifTrue].items.push(
i
);
});
monkey.items = [];
return monkey;
};
const genPlayRound = (monkeys) => {
const max = monkeys.map(R.prop("divisible")).reduce((a, b) => a * b);
return () => monkeys.map((monkey) => playTurn(max, monkeys, monkey));
};
export default R.createPipe(
R.clone,
(monkeys) => R.times(10000, genPlayRound(monkeys)),
R.last(),
R.map(R.prop("inspections")),
R.sortBy(R.identity),
R.reverse,
R.take(2),
R.reduce((a, b) => a * b, 1)
);