-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.test.ts
65 lines (56 loc) · 1.88 KB
/
b.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";
const parse = (input: string): [number, number, number][][] => {
const lines = input.split("\n");
return lines.map((line) => {
return line
.split(": ")[1]
.split("; ")
.map((item) => {
return item.split(", ").reduce(
(acc, colorString) => {
const [amount, color] = colorString.split(" ");
if (color === "red") {
acc[0] += Number(amount);
} else if (color === "green") {
acc[1] += Number(amount);
} else if (color === "blue") {
acc[2] += Number(amount);
}
return acc;
},
[0, 0, 0] as [number, number, number],
);
});
});
};
function solution(input: string) {
const games = parse(input);
const ans = games.reduce((acc, game, index) => {
const minimal = game.reduce((acc, bucket) => {
let colorIdx = 0;
for (const n of bucket) {
if (Math.max(n, acc[colorIdx]) === n) {
acc[colorIdx] = n;
}
colorIdx++;
}
return acc;
});
return minimal.reduce((acc, n) => acc * n, 1) + acc;
}, 0);
return ans;
}
test("example", async () => {
const file = Bun.file("./02/example.txt");
const input = await file.text();
const actual = solution(input);
const expected = 2286;
expect(actual).toBe(expected);
});
test("puzzle input", async () => {
const file = Bun.file("./02/input.txt");
const input = await file.text();
const actual = solution(input);
const expected = 69629;
expect(actual).toBe(expected);
});