-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday09.ts
More file actions
70 lines (60 loc) · 1.84 KB
/
day09.ts
File metadata and controls
70 lines (60 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { readInput } from "./util.ts";
function compact1(disk: number[][]) {
const compacted = disk.flatMap((l) => l);
let j = compacted.length - 1;
for (let i = 0; i < j; i++) {
if (Number.isNaN(compacted[i])) {
if (Number.isNaN(compacted[j])) break;
compacted[i] = compacted[j];
compacted[j] = NaN;
while (Number.isNaN(compacted[--j]));
}
}
compacted.splice(j + 1, compacted.length - j);
return compacted;
}
function compact2(disk: number[][]) {
const compacted = disk.map((l) => [...l]);
for (let j = compacted.length - 1; j >= 0; j -= 2) {
for (let i = 1; i < j; i += 2) {
if (compacted[i].length >= compacted[j].length) {
compacted[i].splice(0, compacted[j].length);
compacted.splice(i, 0, [], [...compacted[j]]);
compacted[j + 1].push(...Array(compacted[j + 2].length).fill(NaN));
compacted[j + 2] = [];
j += 2;
break;
}
}
}
return compacted.flatMap((l) => l);
}
function main(input: string): [number, number] {
const diskmap = input.split("").map(Number);
const checksum = (acc: number, val: number, i: number) =>
Number.isNaN(val) ? acc : acc + val * i;
const disk: number[][] = [];
for (let i = 0; i < diskmap.length; i++) {
const val = i % 2 === 0 ? i / 2 : NaN;
disk.push(Array(diskmap[i]).fill(val));
}
return [
compact1(disk).reduce(checksum, 0),
compact2(disk).reduce(checksum, 0),
];
}
if (import.meta.main) {
const [part1, part2] = main(await readInput());
console.log("Part 1:", part1);
console.log("Part 2:", part2);
}
// Test
import { assertEquals } from "jsr:@std/assert@1.0.8";
Deno.test("Part 1: ", () => {
const [out, _] = main(`2333133121414131402`);
assertEquals(out, 1928);
});
Deno.test("Part 2: ", () => {
const [_, out] = main(`2333133121414131402`);
assertEquals(out, 2858);
});