Skip to content

Commit 7943538

Browse files
committed
2024 day 11 part 2
1 parent b09d32b commit 7943538

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

2024/js/day11.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ const blinkStone = (stone) => {
1111
return [2024 * stone];
1212
};
1313

14-
const blink = (stones) => stones.flatMap(s => blinkStone(s));
14+
const cache = {};
15+
const numberAfterBlinks = (stone, blinks) => {
16+
const key = `${stone}.${blinks}`;
17+
if (cache[key]) return cache[key];
18+
if (blinks == 0) return 1;
19+
const stones = blinkStone(stone);
20+
const ret = stones.map(s => numberAfterBlinks(s, blinks-1)).reduce((a, b) => a + b);
21+
cache[key] = ret;
22+
return ret;
23+
}
1524

16-
let afterBlink = stones;
17-
for (let i = 0; i < 25; i++) {
18-
afterBlink = blink(afterBlink);
25+
const stonesAfterBlinks = (stones, blinks) => {
26+
return stones.map(s => numberAfterBlinks(s, blinks)).reduce((a, b) => a + b);
1927
}
20-
console.log(afterBlink.length);
28+
29+
console.log(`part1: ${stonesAfterBlinks(stones, 25)}`);
30+
console.log(`part2: ${stonesAfterBlinks(stones, 75)}`);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy