-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.test.ts
53 lines (40 loc) · 1.32 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
import { expect, test } from "bun:test";
function solution(input: string) {
const sequences = input.split("\n").map((line) => line.split(" ").map(Number));
let ans = 0;
for (const sequence of sequences) {
const acc = [sequence];
let last = acc[acc.length - 1];
while (!last.every((el) => el === 0)) {
const next = [];
for (let i = 1; i < last.length; i++) {
const el = last[i] - last[i - 1];
next.push(el);
}
acc.push(next);
last = next;
}
const acc1 = acc.map((el) => el[el.length - 1]);
const acc2 = [0];
for (let i = acc1.length - 2; i >= 0; i--) {
const next = acc1[i] + acc2[acc2.length - 1];
acc2.push(next);
}
ans += acc2[acc2.length - 1];
}
return ans;
}
test("example", async () => {
const file = Bun.file(`${import.meta.dir}/example.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 114;
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 = 1702218515;
expect(actual).toBe(expected);
});