adventofcode/2022/11/part2.js

35 lines
760 B
JavaScript
Raw Normal View History

2022-12-11 18:30:44 +00:00
import * as R from "remeda";
2022-12-12 00:32:35 +00:00
const playTurn = (max, monkeys, monkey) => {
monkey.inspections += monkey.items.length;
2022-12-11 18:30:44 +00:00
2022-12-12 00:32:35 +00:00
monkey.items
.map((i) => monkey.operation(i) % max)
.forEach((i) => {
monkeys[i % monkey.divisible ? monkey.ifFalse : monkey.ifTrue].items.push(
i
);
});
2022-12-11 18:30:44 +00:00
2022-12-12 00:32:35 +00:00
monkey.items = [];
2022-12-11 18:30:44 +00:00
2022-12-12 00:32:35 +00:00
return monkey;
};
2022-12-11 18:30:44 +00:00
2022-12-12 00:32:35 +00:00
const genPlayRound = (monkeys) => {
const max = monkeys.map(R.prop("divisible")).reduce((a, b) => a * b);
2022-12-11 18:30:44 +00:00
2022-12-12 00:32:35 +00:00
return () => monkeys.map((monkey) => playTurn(max, monkeys, monkey));
2022-12-11 18:30:44 +00:00
};
export default R.createPipe(
2022-12-12 00:32:35 +00:00
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)
);