Skip to content

Commit eb852d3

Browse files
committed
11/2024
1 parent 7114433 commit eb852d3

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

2024/11/index.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { getPuzzle } from "../../utils";
2+
3+
const puzzleInput = getPuzzle(__dirname).trim();
4+
5+
const stones = puzzleInput.split(" ");
6+
7+
const blink = (
8+
stone: string,
9+
blinkCount: number,
10+
cache: Map<string, number>
11+
) => {
12+
const nextBlinkCount = blinkCount - 1;
13+
14+
if (nextBlinkCount >= 0) {
15+
const cacheKey = `${stone}.${blinkCount}`;
16+
17+
if (cache.has(cacheKey)) {
18+
return cache.get(cacheKey);
19+
}
20+
21+
if (stone === "0") {
22+
const res = blink("1", nextBlinkCount, cache);
23+
cache.set(cacheKey, res);
24+
return res;
25+
}
26+
27+
if (stone.length % 2 === 0) {
28+
const center = stone.length / 2;
29+
30+
const newStone1 = stone.substring(0, center);
31+
const newStone2 = `${Number(stone.substring(center, stone.length))}`;
32+
33+
const res1 = blink(newStone1, nextBlinkCount, cache);
34+
const res2 = blink(newStone2, nextBlinkCount, cache);
35+
const res = res1 + res2;
36+
37+
cache.set(cacheKey, res);
38+
return res;
39+
}
40+
41+
const res = blink(`${Number(stone) * 2024}`, nextBlinkCount, cache);
42+
cache.set(cacheKey, res);
43+
44+
return res;
45+
}
46+
47+
return 1;
48+
};
49+
50+
const countStones = (blinks: number) => {
51+
const cache = new Map<string, number>();
52+
let count = 0;
53+
54+
for (const stone of stones) {
55+
count += blink(stone, blinks, cache);
56+
}
57+
58+
return count;
59+
};
60+
61+
// Part 1
62+
(() => {
63+
console.time("part 1");
64+
const stoneCount = countStones(25);
65+
console.log("part 1 stoneCount >>>", stoneCount);
66+
console.timeEnd("part 1");
67+
})();
68+
69+
// Part 2
70+
(() => {
71+
console.time("part 2");
72+
const stoneCount = countStones(75);
73+
console.log("part 2 stoneCount >>>", stoneCount);
74+
console.timeEnd("part 2");
75+
})();

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
| Day | Part 1 | Part 2 |
88
| :----------------------------------------: | :----: | :----: |
9+
| [11](https://adventofcode.com/2024/day/11) |||
910
| [10](https://adventofcode.com/2024/day/10) |||
1011
| [09](https://adventofcode.com/2024/day/9) |||
1112
| [08](https://adventofcode.com/2024/day/8) |||

0 commit comments

Comments
 (0)