From 275211bf9bdd22ea1014ea5a5fd2b1771e00bbde Mon Sep 17 00:00:00 2001 From: Sven Palberg Date: Sat, 14 Dec 2024 11:36:04 +0100 Subject: [PATCH] day 11 (#24) --- days/11/main.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++ days/11/main_test.ts | 26 ++++++++++++++++++++ days/mod.ts | 2 +- inputs | 2 +- 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 days/11/main.ts create mode 100644 days/11/main_test.ts diff --git a/days/11/main.ts b/days/11/main.ts new file mode 100644 index 0000000..f5c60d0 --- /dev/null +++ b/days/11/main.ts @@ -0,0 +1,58 @@ +import { loadInput } from "utils"; + +export function part1(input: Array) { + const stones = parseInput(input); + const blink25 = blink(25); + return stones.map(blink25).reduce((a, b) => a + b); +} + +export function part2(input: Array) { + const stones = parseInput(input); + const blink75 = blink(75); + return stones.map(blink75).reduce((a, b) => a + b); +} + +function blink(times: number) { + const cache = new Map(); + return (stone: number) => blinkRec(stone, times, cache); +} + +function blinkRec( + stone: number, + times: number, + cache: Map, +): number { + if (times === 0) { + return 1; + } + const cacheHit = cache.get(`${stone}-${times}`); + if (cacheHit != null) { + return cacheHit; + } + if (stone === 0) { + const result = blinkRec(1, times - 1, cache); + cache.set(`${stone}-${times}`, result); + return result; + } + const str = stone.toString(); + if (str.length % 2 === 0) { + const half = str.length / 2; + const left = blinkRec(parseInt(str.slice(0, half), 10), times - 1, cache); + const right = blinkRec(parseInt(str.slice(half), 10), times - 1, cache); + const result = left + right; + cache.set(`${stone}-${times}`, result); + return result; + } + const result = blinkRec(stone * 2024, times - 1, cache); + cache.set(`${stone}-${times}`, result); + return result; +} + +function parseInput(input: Array): Array { + return input[0].split(" ").map(Number); +} + +if (import.meta.main) { + console.log(part1(await loadInput(11))); + console.log(part2(await loadInput(11))); +} diff --git a/days/11/main_test.ts b/days/11/main_test.ts new file mode 100644 index 0000000..b5cafb4 --- /dev/null +++ b/days/11/main_test.ts @@ -0,0 +1,26 @@ +import { expect } from "@std/expect"; +import { describe, it } from "@std/testing/bdd"; +import { part1, part2 } from "./main.ts"; +import { loadInput } from "utils"; + +describe("day 11 example", () => { + const input = ["125 17"]; + + it("part 1", () => { + expect(part1(input)).toBe(55312); + }); + + it("part 2", () => { + expect(part2(input)).toBe(65601038650482); + }); +}); + +describe("day 11 solution", () => { + it("part 1", async () => { + expect(part1(await loadInput(11))).toBe(239714); + }); + + it("part 2", async () => { + expect(part2(await loadInput(11))).toBe(284973560658514); + }); +}); diff --git a/days/mod.ts b/days/mod.ts index 9a86554..620d2e1 100644 --- a/days/mod.ts +++ b/days/mod.ts @@ -3,7 +3,7 @@ type Solution = { part1: PartFunction; part2: PartFunction }; const days = new Map(); -for (const day of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) { +for (const day of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) { days.set(day, await import(`./${day}/main.ts`)); } diff --git a/inputs b/inputs index 22f292f..94fe764 160000 --- a/inputs +++ b/inputs @@ -1 +1 @@ -Subproject commit 22f292f426a84e77e5d9350dd115a2673de6263d +Subproject commit 94fe7640ff0c710170a620f5596acd3738cf303d