mirror of
https://github.com/seigler/aoc2024
synced 2025-07-27 09:06:09 +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,
|
"solved": true,
|
||||||
"result": "213625",
|
"result": "213625",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": 54.460336
|
"time": 4.472301
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": true,
|
"solved": true,
|
||||||
|
@ -170,7 +170,7 @@
|
||||||
"attempts": [
|
"attempts": [
|
||||||
"299307972"
|
"299307972"
|
||||||
],
|
],
|
||||||
"time": 130.250605
|
"time": 126.217947
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,50 +2,33 @@ import run from "aocrunner"
|
||||||
|
|
||||||
const parseInput = (rawInput: string) => rawInput.split(' ').map(Number)
|
const parseInput = (rawInput: string) => rawInput.split(' ').map(Number)
|
||||||
|
|
||||||
function cacheKey(...args) { // good enough
|
const memoize = <T extends unknown[],U>(fn: (...args: T) => U): (...args: T) => U => {
|
||||||
return args.map(String).join('\n');
|
const cache = {} // Saves the values
|
||||||
}
|
|
||||||
function memoize<T extends unknown[],U>(fn: (...args: T) => U): (...args: T) => U {
|
|
||||||
const cache = {}; // Saves the values
|
|
||||||
return (...args) => {
|
return (...args) => {
|
||||||
const key = cacheKey(...args);
|
const key = args.join('\n')
|
||||||
if (!cache[key]) { // Not in the cache? Call fn.
|
return cache[key] ?? (cache[key] = fn(...args))
|
||||||
cache[key] = fn(...args); // Now it's in the cache.
|
|
||||||
}
|
|
||||||
return cache[key];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 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 part1 = (rawInput: string) => {
|
||||||
const input = parseInput(rawInput)
|
const input = parseInput(rawInput)
|
||||||
const blink = (n: number, turn: number): number[] => {
|
return input.reduce((total, s) => total + count(s, 25), 0)
|
||||||
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 part2 = (rawInput: string) => {
|
||||||
const input = parseInput(rawInput)
|
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)
|
return input.reduce((total, s) => total + count(s, 75), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue