This commit is contained in:
Joshua Seigler 2024-12-11 00:39:43 -05:00
parent 20bcd88600
commit d5950dd5e9
4 changed files with 100 additions and 16 deletions

View file

@ -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
}
},
{

View file

@ -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
```
<!--/RESULTS-->

9
src/day11/README.md Normal file
View file

@ -0,0 +1,9 @@
# 🎄 Advent of Code 2024 - day 11 🎄
## Info
Task description: [link](https://adventofcode.com/2024/day/11)
## Notes
...

73
src/day11/index.ts Normal file
View file

@ -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<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 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,
})