Skip to content

Commit

Permalink
feat(js): add solutions for 2024 day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Dec 11, 2024
1 parent 64d014f commit 7a0dc98
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
39 changes: 39 additions & 0 deletions js/src/2024/11/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { solution, sum } from '../../utils/index.js';

const parse = (input) => input.trim().split(' ').map(Number);

const blink = (stone, { times, cache = new Map() }) => {
// A stone is just a single stone at one point
if (times === 0) {
return 1;
}

const hash = `${stone}x${times}`;
if (cache.has(hash)) {
return cache.get(hash);
}

const opts = { times: times - 1, cache };

const str = String(stone);
let result;
if (stone === 0) {
result = blink(1, opts);
} else if (str.length % 2 === 0) {
result = blink(+str.slice(0, str.length / 2), opts) + blink(+str.slice(str.length / 2), opts);
} else {
result = blink(stone * 2024, opts);
}
cache.set(hash, result);
return result;
};

export const partOne = solution((input, { blinks: times = 25 }) => {
const stones = parse(input);
return sum(stones.map((stone) => blink(stone, { times })));
});

export const partTwo = solution((input, { blinks: times = 75 }) => {
const stones = parse(input);
return sum(stones.map((stone) => blink(stone, { times })));
});
17 changes: 17 additions & 0 deletions puzzles/2024/11/examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
part-one:
- input: |
0 1 10 99 999
answer: 7
args:
blinks: 1
- input: &example1 |
125 17
answer: 22
args:
blinks: 6

- input: *example1
answer: 55312
args:
blinks: 25

0 comments on commit 7a0dc98

Please sign in to comment.