-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.test.ts
62 lines (49 loc) · 1.51 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
import { expect, test } from "bun:test";
function solution(input: string) {
const [instruction, graphString] = input.split("\n\n");
const graph = Object.fromEntries(
graphString.split("\n").map((line) => {
const [key, value] = line.split(" = ");
const matcher = value.match(/\w+/g);
if (!matcher) throw new Error("Wrong input");
return [key, matcher];
}),
);
let ans = 0;
let current = "AAA";
while (current !== "ZZZ") {
for (const dir of instruction) {
if (current === "ZZZ") {
return ans;
}
if (dir === "R") {
current = graph[current][1];
} else {
current = graph[current][0];
}
ans += 1;
}
}
return ans;
}
test("example-1", async () => {
const file = Bun.file(`${import.meta.dir}/example-1.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 2;
expect(actual).toBe(expected);
});
test("example-2", async () => {
const file = Bun.file(`${import.meta.dir}/example-2.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 6;
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 = 19951;
expect(actual).toBe(expected);
});