From 60c4cdd2c09717492991656bcab9428197a2f9c2 Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Sun, 8 Dec 2024 23:54:16 -0700 Subject: [PATCH 1/2] Day 9 --- day9.peggy | 3 ++ day9.ts | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ inputs | 2 +- test/day9.js | 1 + 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 day9.peggy create mode 100644 day9.ts create mode 100644 test/day9.js diff --git a/day9.peggy b/day9.peggy new file mode 100644 index 0000000..e4fdfc8 --- /dev/null +++ b/day9.peggy @@ -0,0 +1,3 @@ +lines = @num+ "\n" + +num = n:$[0-9] { return parseInt(n, 10) } diff --git a/day9.ts b/day9.ts new file mode 100644 index 0000000..8f3a258 --- /dev/null +++ b/day9.ts @@ -0,0 +1,114 @@ +import { type MainArgs, parseFile } from './lib/utils.ts'; + +type Parsed = number[]; + +function genBlocks(inp: Parsed): [blocks: number[], file: number] { + const blocks: number[] = []; + let file = 0; + let free = false; + let block = 0; + for (const n of inp) { + if (free) { + for (let i=0; i i) { + file = blocks[j]; + delete blocks[j]; + if (!isNaN(file)) { + break; + } + j--; + } + if (i < j) { + blocks[i] = file; + } else { + break; + } + } + + return blocks.reduce((t, v, n) => t + (v === undefined ? 0 : v * n), 0); +} + +function part2(inp: Parsed): number { + const [blocks, maxFile] = genBlocks(inp); + let initial = 0; + + for (let file = maxFile; file >= 0; file--) { + // TODO: move to lastIndexOf + const start = blocks.indexOf(file, initial); + if (start === -1) { + continue; + } + let end = start; + while (end < blocks.length) { + if (blocks[++end] !== file) { + break; + } + } + + const len = end - start; + let i = initial; + initial = NaN; + + OUTER: + while (i < start) { + while (i < start) { + if (isNaN(blocks[i])) { + if (isNaN(initial)) { + initial = i; + } + break; + } + i++; + } + if (i < start) { + for (let x = i; x < i + len; x++) { + if (!isNaN(blocks[x])) { + i = x; + continue OUTER; + } + } + break; + } + } + if (i < start) { + for (let x = i; x < i + len; x++) { + blocks[x] = file; + blocks[start + x - i] = NaN; + } + i += len; + } + } + + return blocks.reduce((t, v, n) => t + (isNaN(v) ? 0 : v * n), 0); +} + +export default async function main(args: MainArgs): Promise<[number, number]> { + const inp = await parseFile(args); + return [part1(inp), part2(inp)]; +} diff --git a/inputs b/inputs index 7f179a5..6896ce8 160000 --- a/inputs +++ b/inputs @@ -1 +1 @@ -Subproject commit 7f179a501ebc3cba464c14c60f099ce151cf0089 +Subproject commit 6896ce8bdd5ac779a2236ff1c92a5be86c87379b diff --git a/test/day9.js b/test/day9.js new file mode 100644 index 0000000..a4eb94d --- /dev/null +++ b/test/day9.js @@ -0,0 +1 @@ +export default [6301895872542, 6323761685944]; From 2e69ced294a94cc11075c50c0ad886bd785c3376 Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Sun, 8 Dec 2024 23:55:03 -0700 Subject: [PATCH 2/2] lint --- day9.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/day9.ts b/day9.ts index 8f3a258..21cc45e 100644 --- a/day9.ts +++ b/day9.ts @@ -9,11 +9,11 @@ function genBlocks(inp: Parsed): [blocks: number[], file: number] { let block = 0; for (const n of inp) { if (free) { - for (let i=0; i= 0; file--) { - // TODO: move to lastIndexOf + // TODO(hildjj): move to lastIndexOf const start = blocks.indexOf(file, initial); if (start === -1) { continue;