-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.test.ts
45 lines (36 loc) · 1.06 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
import { expect, test } from "bun:test";
function solution(input: string) {
const [time, distance] = input.split("\n").map((s) => {
const result = s.match(/\d+/g);
if (!result) throw new Error();
return result.map(Number);
});
let ans = 1;
for (let i = 0; i < time.length; i++) {
const t = time[i];
const d = distance[i];
const s = (v: number, t: number): number => v * t;
let counter = 0;
for (let j = 1; j < t; j++) {
if (s(j, t - j) > d) {
counter += 1;
}
}
ans *= counter;
}
return ans;
}
test("example", async () => {
const file = Bun.file("./06/example.txt");
const input = await file.text();
const actual = solution(input);
const expected = 288;
expect(actual).toBe(expected);
});
test("puzzle input", async () => {
const file = Bun.file("./06/input.txt");
const input = await file.text();
const actual = solution(input);
const expected = 500346;
expect(actual).toBe(expected);
});