From d447c97f4b22f10d1d68b7a143ba3198929ed73e Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Mon, 9 Dec 2024 02:18:15 -0500 Subject: [PATCH] day 9 --- .aocrunner.json | 21 +++++++----- README.md | 12 +++---- src/day09/README.md | 9 ++++++ src/day09/index.ts | 78 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 14 deletions(-) create mode 100644 src/day09/README.md create mode 100644 src/day09/index.ts diff --git a/.aocrunner.json b/.aocrunner.json index 64e0955..8a2e086 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -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 } }, { diff --git a/README.md b/README.md index 86b47a7..a975eb6 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ [![Day](https://badgen.net/badge/06/%E2%98%85%E2%98%85/green)](src/day06) [![Day](https://badgen.net/badge/07/%E2%98%85%E2%98%85/green)](src/day07) [![Day](https://badgen.net/badge/08/%E2%98%85%E2%98%85/green)](src/day08) -![Day](https://badgen.net/badge/09/%E2%98%86%E2%98%86/gray) +[![Day](https://badgen.net/badge/09/%E2%98%85%E2%98%85/green)](src/day09) ![Day](https://badgen.net/badge/10/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/11/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/12/%E2%98%86%E2%98%86/gray) @@ -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 ``` diff --git a/src/day09/README.md b/src/day09/README.md new file mode 100644 index 0000000..73c1160 --- /dev/null +++ b/src/day09/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2024 - day 9 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2024/day/9) + +## Notes + +... \ No newline at end of file diff --git a/src/day09/index.ts b/src/day09/index.ts new file mode 100644 index 0000000..1c3a39e --- /dev/null +++ b/src/day09/index.ts @@ -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, +})