-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.test.ts
78 lines (69 loc) · 2.42 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { expect, test } from "bun:test";
function solution(input: string) {
const [workflowsString, partsString] = input.split("\n\n");
const workflows = workflowsString
.split("\n")
.reduce<Record<string, [["x" | "m" | "a" | "s", ">" | "<", number, string][], string]>>((acc, line) => {
const [key, value] = line.split("{");
const rules = value.slice(0, -1).split(",");
const defaultDest = rules.pop();
if (!defaultDest) {
throw new Error("Unexpected");
}
acc[key] = [[], defaultDest];
for (const rule of rules) {
const [comparison, to] = rule.split(":");
const category = comparison[0] as "x" | "m" | "a" | "s";
const op = comparison[1] as ">" | "<";
const n = Number(comparison.slice(2));
acc[key][0].push([category, op, n, to]);
}
return acc;
}, {});
const parts = partsString.split("\n").map((line) => {
const [x, m, a, s] = line
.slice(1, -1)
.split(",")
.map((p) => Number(p.slice(2)));
return { x, m, a, s };
});
let ans = 0;
for (const part of parts) {
let workflowKey = "in";
while (true) {
if (workflowKey === "A") {
ans += part.x + part.m + part.a + part.s;
break;
}
if (workflowKey === "R") {
break;
}
const workflow = workflows[workflowKey];
const rules = workflow[0];
let nextWorkflowKey;
for (const rule of rules) {
const cond = eval(`${part[rule[0]]} ${rule[1]} ${rule[2]}`);
if (cond) {
nextWorkflowKey = rule[3];
break;
}
}
workflowKey = nextWorkflowKey ?? workflow[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 = 19114;
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 = 330820;
expect(actual).toBe(expected);
});