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