From 7a0dc981c76ce7949b6594e3a81d733f0a8c6463 Mon Sep 17 00:00:00 2001 From: Tim Kurvers Date: Wed, 11 Dec 2024 12:41:21 +0100 Subject: [PATCH] feat(js): add solutions for 2024 day 11 --- js/src/2024/11/index.js | 39 +++++++++++++++++++++++++++++++++++ puzzles/2024/11/examples.yaml | 17 +++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 js/src/2024/11/index.js create mode 100644 puzzles/2024/11/examples.yaml diff --git a/js/src/2024/11/index.js b/js/src/2024/11/index.js new file mode 100644 index 0000000..a955537 --- /dev/null +++ b/js/src/2024/11/index.js @@ -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 }))); +}); diff --git a/puzzles/2024/11/examples.yaml b/puzzles/2024/11/examples.yaml new file mode 100644 index 0000000..c92ca57 --- /dev/null +++ b/puzzles/2024/11/examples.yaml @@ -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