-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.test.ts
65 lines (52 loc) · 1.56 KB
/
a.test.ts
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
import { expect, test } from "bun:test";
function zip(arr: string[]): string[] {
return Array.from({ length: arr[0].length }, (_, i) => arr.map((a) => a[i]).join(""));
}
function findMirrorIndex(arr: string[]): number | undefined {
for (let i = 1; i < arr.length; i++) {
const a = arr.slice(0, i).reverse();
const b = arr.slice(i);
if (a.length < b.length) {
b.length = a.length;
} else {
a.length = b.length;
}
if (Bun.deepEquals(a, b)) {
return i;
}
}
}
function solution(input: string) {
const records = input.split("\n\n").map((block) => block.split("\n"));
let left = 0;
let top = 0;
for (const record of records) {
const row = findMirrorIndex(record);
if (row !== undefined) {
top += row;
continue;
}
const zipped = zip(record);
const col = findMirrorIndex(zipped);
if (col !== undefined) {
left += col;
continue;
}
throw new Error("Unexpected");
}
return left + 100 * top;
}
test("example", async () => {
const file = Bun.file(`${import.meta.dir}/example.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 405;
expect(actual).toBe(expected);
});
test("puzzle input", async () => {
const file = Bun.file(`${import.meta.dir}/input.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 30802;
expect(actual).toBe(expected);
});