From d5950dd5e9dd6722725c82fd121af9f2158f41e0 Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Wed, 11 Dec 2024 00:39:43 -0500 Subject: [PATCH] day 11 --- .aocrunner.json | 16 +++++----- README.md | 18 +++++------ src/day11/README.md | 9 ++++++ src/day11/index.ts | 73 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 src/day11/README.md create mode 100644 src/day11/index.ts diff --git a/.aocrunner.json b/.aocrunner.json index e83d838..4d51d06 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -159,16 +159,18 @@ }, { "part1": { - "solved": false, - "result": null, + "solved": true, + "result": "213625", "attempts": [], - "time": null + "time": 54.460336 }, "part2": { - "solved": false, - "result": null, - "attempts": [], - "time": null + "solved": true, + "result": "252442982856820", + "attempts": [ + "299307972" + ], + "time": 130.250605 } }, { diff --git a/README.md b/README.md index 09eb59d..cfc84f5 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ [![Day](https://badgen.net/badge/08/%E2%98%85%E2%98%85/green)](src/day08) [![Day](https://badgen.net/badge/09/%E2%98%85%E2%98%85/green)](src/day09) [![Day](https://badgen.net/badge/10/%E2%98%85%E2%98%85/green)](src/day10) -![Day](https://badgen.net/badge/11/%E2%98%86%E2%98%86/gray) +[![Day](https://badgen.net/badge/11/%E2%98%85%E2%98%85/green)](src/day11) ![Day](https://badgen.net/badge/12/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/13/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/14/%E2%98%86%E2%98%86/gray) @@ -132,16 +132,16 @@ Both parts: 288.449ms ``` Day 10 -Time part 1: 6.148ms -Time part 2: 4.028ms -Both parts: 10.176ms +Time part 1: 6.147ms +Time part 2: 4.869ms +Both parts: 11.016ms ``` ``` Day 11 -Time part 1: - -Time part 2: - -Both parts: - +Time part 1: 54.46ms +Time part 2: 130.251ms +Both parts: 184.711ms ``` ``` @@ -243,8 +243,8 @@ Both parts: - ``` ``` -Total stars: 20/50 -Total time: 38383.499ms +Total stars: 22/50 +Total time: 38569.05ms ``` diff --git a/src/day11/README.md b/src/day11/README.md new file mode 100644 index 0000000..0d37e6e --- /dev/null +++ b/src/day11/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2024 - day 11 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2024/day/11) + +## Notes + +... \ No newline at end of file diff --git a/src/day11/index.ts b/src/day11/index.ts new file mode 100644 index 0000000..68c0014 --- /dev/null +++ b/src/day11/index.ts @@ -0,0 +1,73 @@ +import run from "aocrunner" + +const parseInput = (rawInput: string) => rawInput.split(' ').map(Number) + +function cacheKey(...args) { // good enough + return args.map(String).join('\n'); +} +function memoize(fn: (...args: T) => U): (...args: T) => U { + const cache = {}; // Saves the values + return (...args) => { + const key = cacheKey(...args); + if (!cache[key]) { // Not in the cache? Call fn. + cache[key] = fn(...args); // Now it's in the cache. + } + return cache[key]; + } +} + +const part1 = (rawInput: string) => { + const input = parseInput(rawInput) + const blink = (n: number, turn: number): number[] => { + if (turn === 0) return [n] + if (n === 0) return blink(1, turn - 1) + const digits = Math.floor(Math.log10(n)) + 1 + if (digits % 2 === 0) { + const exp = 10**(digits/2) + return [ + ...blink(Math.floor(n / exp), turn - 1), + ...blink(n % exp, turn - 1) + ] + } + return blink(n * 2024, turn - 1) + } + return input.flatMap(s => blink(s, 25)).length +} + +const part2 = (rawInput: string) => { + const input = parseInput(rawInput) + const count = memoize((n: number, turn: number): number => { + if (turn === 0) return 1 + if (n === 0) return count(1, turn - 1) + const digits = Math.floor(Math.log10(n)) + 1 + if (digits % 2 === 0) { + const exp = 10**(digits/2) + return count(Math.floor(n / exp), turn - 1) + count(n % exp, turn - 1) + } + return count(n * 2024, turn - 1) + }) + return input.reduce((total, s) => total + count(s, 75), 0) +} + +run({ + part1: { + tests: [ + // { + // input: ``, + // expected: "", + // }, + ], + solution: part1, + }, + part2: { + tests: [ + // { + // input: ``, + // expected: "", + // }, + ], + solution: part2, + }, + trimTestInputs: true, + onlyTests: false, +})