-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.ts
78 lines (72 loc) · 1.9 KB
/
part1.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 * as fs from 'fs';
const input = fs
.readFileSync('input', 'utf8')
.split('\n\n')
.map((x) => x.split('\n'));
globalThis.workflows = {};
const { workflows } = globalThis;
const parts = input[1].map((x) => ({
...JSON.parse(x.replace('{', '{"').replace(/=/g, '":').replace(/,/g, ',"')),
accepted: false,
}));
for (const line of input[0]) {
const [workflow, funcs] = line
.slice(0, -1)
.split('{')
.map((x, i) => (i == 1 ? x.split(',') : x));
let functionString = '';
for (const func of funcs) {
if (func.includes('>')) {
const [greater, than] = func.split('>').map((x, i) => (i == 1 ? x.split(':')[0] : x));
functionString += `if (part.${greater} > ${than}) return`;
const then = func.split(':')[1];
switch (then) {
case 'A':
functionString += ' part.accepted = true;\n';
break;
case 'R':
functionString += ';\n';
break;
default:
functionString += ` globalThis.workflows.${then}(part);\n`;
break;
}
} else if (func.includes('<')) {
const [less, than] = func.split('<').map((x, i) => (i == 1 ? x.split(':')[0] : x));
functionString += `if (part.${less} < ${than}) return`;
const then = func.split(':')[1];
switch (then) {
case 'A':
functionString += ' part.accepted = true;\n';
break;
case 'R':
functionString += ';\n';
break;
default:
functionString += ` globalThis.workflows.${then}(part);\n`;
break;
}
} else {
switch (func) {
case 'A':
functionString += 'return part.accepted = true;';
break;
case 'R':
functionString += 'return;';
break;
default:
functionString += `return globalThis.workflows.${func}(part);`;
break;
}
}
}
workflows[workflow] = new Function('part', functionString);
}
let result = 0;
for (const part of parts) {
workflows.in(part);
if (part.accepted) {
result += part.x + part.m + part.a + part.s;
}
}
console.log(result);