-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.js
30 lines (26 loc) · 961 Bytes
/
day11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const stones = '572556 22 0 528 4679021 1 10725 2790'.split(' ').map(s => Number(s));
const blinkStone = (stone) => {
if (stone == 0) return [1];
const str = `${stone}`;
if (str.length % 2 == 0) {
const middle = str.length / 2;
const [left, right] = [str.substring(0, middle), str.substring(middle)];
return [Number(left), Number(right)];
}
return [2024 * stone];
};
const cache = {};
const numberAfterBlinks = (stone, blinks) => {
const key = `${stone}.${blinks}`;
if (cache[key]) return cache[key];
if (blinks == 0) return 1;
const stones = blinkStone(stone);
const ret = stones.map(s => numberAfterBlinks(s, blinks-1)).reduce((a, b) => a + b);
cache[key] = ret;
return ret;
}
const stonesAfterBlinks = (stones, blinks) => {
return stones.map(s => numberAfterBlinks(s, blinks)).reduce((a, b) => a + b);
}
console.log(`part1: ${stonesAfterBlinks(stones, 25)}`);
console.log(`part2: ${stonesAfterBlinks(stones, 75)}`);