mirror of
https://github.com/seigler/aoc2024
synced 2025-07-26 00:36:10 +00:00
day 11, cleaner
This commit is contained in:
parent
d5950dd5e9
commit
52437aeb8a
2 changed files with 19 additions and 36 deletions
|
@ -162,7 +162,7 @@
|
|||
"solved": true,
|
||||
"result": "213625",
|
||||
"attempts": [],
|
||||
"time": 54.460336
|
||||
"time": 4.472301
|
||||
},
|
||||
"part2": {
|
||||
"solved": true,
|
||||
|
@ -170,7 +170,7 @@
|
|||
"attempts": [
|
||||
"299307972"
|
||||
],
|
||||
"time": 130.250605
|
||||
"time": 126.217947
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -2,50 +2,33 @@ 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<T extends unknown[],U>(fn: (...args: T) => U): (...args: T) => U {
|
||||
const cache = {}; // Saves the values
|
||||
const memoize = <T extends unknown[],U>(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 key = args.join('\n')
|
||||
return cache[key] ?? (cache[key] = fn(...args))
|
||||
}
|
||||
}
|
||||
|
||||
/** How many stones will a stone marked `n` be after `turn` blinks? */
|
||||
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)
|
||||
})
|
||||
|
||||
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
|
||||
return input.reduce((total, s) => total + count(s, 25), 0)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue