mirror of
https://github.com/seigler/aoc2024
synced 2025-07-26 00:36:10 +00:00
day 9
This commit is contained in:
parent
8ca6f75820
commit
d447c97f4b
4 changed files with 106 additions and 14 deletions
|
@ -126,16 +126,21 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"solved": true,
|
||||
"result": "6370402949053",
|
||||
"attempts": [
|
||||
"90885620042",
|
||||
"90582588300"
|
||||
],
|
||||
"time": 236.521292
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"solved": true,
|
||||
"result": "6398096697992",
|
||||
"attempts": [
|
||||
"8553289251139"
|
||||
],
|
||||
"time": 51.927938
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
12
README.md
12
README.md
|
@ -19,7 +19,7 @@
|
|||
[](src/day06)
|
||||
[](src/day07)
|
||||
[](src/day08)
|
||||

|
||||
[](src/day09)
|
||||

|
||||

|
||||

|
||||
|
@ -125,9 +125,9 @@ Both parts: 1.863ms
|
|||
|
||||
```
|
||||
Day 09
|
||||
Time part 1: -
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Time part 1: 239.635ms
|
||||
Time part 2: 53.494ms
|
||||
Both parts: 293.129ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 16/50
|
||||
Total time: 38084.874ms
|
||||
Total stars: 18/50
|
||||
Total time: 38378.002ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day09/README.md
Normal file
9
src/day09/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2024 - day 9 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2024/day/9)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
78
src/day09/index.ts
Normal file
78
src/day09/index.ts
Normal file
|
@ -0,0 +1,78 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput: string) => rawInput.split('').map(Number)
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
let mem: number[] = []
|
||||
let space = 0
|
||||
for (let i = 0; i < input.length; i+= 2) {
|
||||
const [f,s] = input.slice(i,i+2)
|
||||
mem.push(...(new Array(f).fill(i/2)))
|
||||
mem.push(...(new Array(s).fill(-1)))
|
||||
space += s
|
||||
}
|
||||
for (let from = mem.length - 1; from >= 0; from--) {
|
||||
if (mem[from] < 0) continue
|
||||
const to = mem.findIndex(x => x < 0)
|
||||
if (to === -1 || to > from) break
|
||||
mem[to] = mem[from]
|
||||
mem[from] = -1
|
||||
}
|
||||
return mem.reduce((total, b, i) => {
|
||||
if (b < 0) return total
|
||||
return total + b * i
|
||||
}, 0)
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
let files: number[][] = [] // [index, length]
|
||||
let spaces: number[][] = []
|
||||
for (let i = 0, cursor = 0; i < input.length; i+= 2) {
|
||||
const [f,s] = input.slice(i,i+2)
|
||||
files.push([cursor, f])
|
||||
cursor += f
|
||||
if (s === undefined) continue
|
||||
spaces.push([cursor, s])
|
||||
cursor += s
|
||||
}
|
||||
for (let i = files.length - 1; i >= 0; i--) {
|
||||
const file = files[i]
|
||||
const space = spaces.find(space => space[1] >= file[1])
|
||||
spaces.pop() // remove the space to the left of this file, which can no longer be used
|
||||
if (space === undefined) {
|
||||
continue
|
||||
}
|
||||
space[1] -= file[1]
|
||||
const newSpaceIndex = space[0] + file[1]
|
||||
file[0] = space[0]
|
||||
space[0] = newSpaceIndex
|
||||
}
|
||||
return files.reduce((total, [index,length], id) => {
|
||||
return total + id * length * (2*index + length - 1) / 2
|
||||
}, 0)
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `2333133121414131402`,
|
||||
expected: 1928,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: `2333133121414131402`,
|
||||
expected: 2858,
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue