-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.test.ts
47 lines (41 loc) · 1.11 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
import { expect, test } from "bun:test";
function solution(input: string) {
const lines = input.split("\n");
const ans = lines.reduce((acc, line) => {
let i = 0;
let j = line.length - 1;
let d = "";
while (i < line.length) {
const symb = line[i];
if (symb >= "0" && symb <= "9") {
d += symb;
break;
}
i++;
}
while (j > -1) {
const symb = line[j];
if (symb >= "0" && symb <= "9") {
d += symb;
break;
}
j--;
}
return acc + Number(d);
}, 0);
return ans;
}
test("example-a", async () => {
const file = Bun.file("./01/example-a.txt");
const input = await file.text();
const actual = solution(input);
const expected = 142;
expect(actual).toBe(expected);
});
test("puzzle input", async () => {
const file = Bun.file("./01/input.txt");
const input = await file.text();
const actual = solution(input);
const expected = 53194;
expect(actual).toBe(expected);
});