From 2960b89991f1b1ef3535b9f498836f992ee83752 Mon Sep 17 00:00:00 2001 From: Wiktoria Van Harneveldt Date: Wed, 11 Dec 2024 20:44:22 +0100 Subject: [PATCH] d11 p2 --- src/11.ts | 38 +++++++++++++++++++++++++++++++++++++- test/11.test.ts | 2 +- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/11.ts b/src/11.ts index 97b84d0..f1895ca 100644 --- a/src/11.ts +++ b/src/11.ts @@ -4,6 +4,7 @@ function solution(input: string): { part1: string; part2: string } { const lines = input.split(" "); const totalBlinkAmount = 25; + const totalBlinkAmount2 = 75; let stones: string[] = [...lines]; for (let i = 0; i < totalBlinkAmount; i++) { @@ -20,9 +21,44 @@ function solution(input: string): { part1: string; part2: string } { return (Number(line) * 2024).toString(); }); } - result1 = stones.length; + const cache = new Map(); + + function ans(stone: number, iter: number): number { + if (iter === 0) { + return 1; + } + + if (cache.has(`${stone},${iter}`)) { + return cache.get(`${stone},${iter}`)!; + } + + let res = 0; + + if (stone === 0) { + res = ans(1, iter - 1); + } else if (stone.toString().length % 2 === 0) { + const st = stone.toString(); + const half = st.length / 2; + const firstHalf = st.slice(0, half); + const secondHalf = st.slice(half); + + res = 0; + res += ans(Number(firstHalf), iter - 1); + res += ans(Number(secondHalf), iter - 1); + } else { + res = ans(stone * 2024, iter - 1); + } + cache.set(`${stone},${iter}`, res); + + return cache.get(`${stone},${iter}`)!; + } + + lines.map(Number).forEach((line) => { + result2 += ans(line, totalBlinkAmount2); + }); + return { part1: result1.toString(), part2: result2.toString(), diff --git a/test/11.test.ts b/test/11.test.ts index 3df0d22..edd65ce 100644 --- a/test/11.test.ts +++ b/test/11.test.ts @@ -5,7 +5,7 @@ const EXAMPLE_1 = `125 17`; const RESULT_1 = "55312"; const EXAMPLE_2 = EXAMPLE_1; -const RESULT_2 = "31"; +const RESULT_2 = "65601038650482"; describe("Day 11", () => { test("Part 1", () => {