mirror of
https://github.com/seigler/aoc2024
synced 2025-07-27 00:56:10 +00:00
day 11
This commit is contained in:
parent
20bcd88600
commit
d5950dd5e9
4 changed files with 100 additions and 16 deletions
|
@ -159,16 +159,18 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "213625",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 54.460336
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "252442982856820",
|
||||||
"attempts": [],
|
"attempts": [
|
||||||
"time": null
|
"299307972"
|
||||||
|
],
|
||||||
|
"time": 130.250605
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
18
README.md
18
README.md
|
@ -21,7 +21,7 @@
|
||||||
[](src/day08)
|
[](src/day08)
|
||||||
[](src/day09)
|
[](src/day09)
|
||||||
[](src/day10)
|
[](src/day10)
|
||||||

|
[](src/day11)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -132,16 +132,16 @@ Both parts: 288.449ms
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 10
|
Day 10
|
||||||
Time part 1: 6.148ms
|
Time part 1: 6.147ms
|
||||||
Time part 2: 4.028ms
|
Time part 2: 4.869ms
|
||||||
Both parts: 10.176ms
|
Both parts: 11.016ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 11
|
Day 11
|
||||||
Time part 1: -
|
Time part 1: 54.46ms
|
||||||
Time part 2: -
|
Time part 2: 130.251ms
|
||||||
Both parts: -
|
Both parts: 184.711ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 20/50
|
Total stars: 22/50
|
||||||
Total time: 38383.499ms
|
Total time: 38569.05ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
9
src/day11/README.md
Normal file
9
src/day11/README.md
Normal 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
73
src/day11/index.ts
Normal 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,
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue