there's an off-by-one error in here somewhere
This commit is contained in:
50
09/index.ts
Normal file
50
09/index.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import fs from "node:fs";
|
||||||
|
import { printTime, now, toNum } from "../util";
|
||||||
|
|
||||||
|
const test = true;
|
||||||
|
|
||||||
|
const data = fs.readFileSync(
|
||||||
|
test ? "./inputs/testinput" : "./inputs/input",
|
||||||
|
"utf8"
|
||||||
|
);
|
||||||
|
|
||||||
|
let timer = now.instant();
|
||||||
|
console.log(
|
||||||
|
"part one:",
|
||||||
|
data
|
||||||
|
.split("\n")
|
||||||
|
.slice(0, -1)[0]
|
||||||
|
.split("")
|
||||||
|
.map(toNum)
|
||||||
|
.reduce((disk, curr, index) => {
|
||||||
|
const fill = index % 2 === 0 ? index / 2 : -1; // -1 represents free space
|
||||||
|
return [...disk, ...Array.from({ length: curr }, () => fill)];
|
||||||
|
}, [] as Array<number>)
|
||||||
|
.reduce((disk, current, i, a) => {
|
||||||
|
console.log(disk, current, i, a);
|
||||||
|
if (current === -1) {
|
||||||
|
while (true) {
|
||||||
|
let tail = a.pop();
|
||||||
|
if (tail === -1 || tail === undefined) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
return [...disk, tail];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return [...disk, current];
|
||||||
|
}
|
||||||
|
}, [] as Array<number>)
|
||||||
|
.reduce((checksum, curr, i) => {
|
||||||
|
console.log(curr, i);
|
||||||
|
return checksum + curr * i;
|
||||||
|
}, 0),
|
||||||
|
printTime(now.instant().since(timer))
|
||||||
|
);
|
||||||
|
|
||||||
|
// timer = now.instant();
|
||||||
|
// console.log(
|
||||||
|
// "part two:",
|
||||||
|
// data.split("\n").slice(0, -1),
|
||||||
|
// printTime(now.instant().since(timer))
|
||||||
|
// );
|
Reference in New Issue
Block a user