Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 22, 2024
1 parent 28c9f78 commit b310894
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/2024/day22.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ export function part1(input) {
export function part2(input) {
let numbers = input.split("\n").map(BigInt);
let cache = new Map();
let max = 0;
for (let prev of numbers) {
let visited = new Set();
let diffs = [];
for (let i = 0; i < 2000; i++) {
let next = hash(prev);
diffs.push(Number((next % 10n) - (prev % 10n)));
prev = next;
if (diffs.length >= 4) {
let key = diffs.slice(-4).join(",");
if (!visited.has(key)) {
let sum = (cache.get(key) || 0) + Number(next % 10n);
cache.set(key, sum);
visited.add(key);
}
}
if (diffs.length < 4) continue;

let key = diffs.join(",");
diffs.shift();
if (visited.has(key)) continue;

let sum = (cache.get(key) || 0) + Number(next % 10n);
max = Math.max(max, sum);
cache.set(key, sum);
visited.add(key);
}
}
return Math.max(...cache.values());
return max;
}

0 comments on commit b310894

Please sign in to comment.