Prevent recalculating common multiple

This commit is contained in:
Ben Ashton 2022-12-11 23:45:41 -07:00
parent 54caefb22e
commit 02fb31d09d

View File

@ -122,13 +122,14 @@ console.log(
`Level of monkey business after 20 rounds: ${monkeyBusinessLevel}`
);
monkeys = getMonkeys(item => {
const commonMultiple = [...monkeys.values()]
.map(monkey => monkey.test.divisibleBy)
.reduce((total, divisibleBy) => total * divisibleBy);
let commonMultiple;
monkeys = getMonkeys(
// Reduce item by commonMultiple whenever it exceeds that value
return item.worryLevel = item.worryLevel % commonMultiple;
});
item => item.worryLevel = item.worryLevel % commonMultiple
);
commonMultiple = [...monkeys.values()]
.map(monkey => monkey.test.divisibleBy)
.reduce((total, divisibleBy) => total * divisibleBy);
simulateRounds(monkeys, 10000);
monkeyBusinessLevel = calculateMonkeyBusinessLevel(monkeys);
console.log(