Skip to content

Commit

Permalink
small optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 22, 2024
1 parent 3247079 commit e796a21
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/2024/day22.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ export function part2(input) {
numbers = numbers.map((prev, j) => {
let next = hash(prev);
diffs[j].push(Number((next % 10n) - (prev % 10n)));
if (diffs[j].length >= 4) {
let key = diffs[j].slice(-4).join(",");
if (diffs[j].length === 4) {
let key = diffs[j].join(",");
let value = cache.get(key) || { sum: 0, set: new Set() };
if (!value.set.has(j)) {
value.set.add(j);
value.sum += Number(next % 10n);
max = Math.max(max, value.sum);
}
cache.set(key, value);
diffs[j].shift();
}
return next;
});
Expand Down

0 comments on commit e796a21

Please sign in to comment.