-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.ts
66 lines (57 loc) · 1.64 KB
/
part2.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
import * as fs from 'fs';
const input = fs.readFileSync('input', 'utf8').split('\n\n')[0].split('\n');
const workflows = {};
for (const line of input) {
const info = line.split('{');
workflows[info[0]] = info[1]
.slice(0, -1)
.split(',')
.map((x) => {
if (x.includes('>')) {
const [category, value] = x
.split('>')
.map((x, i) => (i == 1 ? Number(x.split(':')[0]) : 'xmas'.indexOf(x)));
return [category, '>', value, x.split(':')[1]];
} else if (x.includes('<')) {
const [category, value] = x
.split('<')
.map((x, i) => (i == 1 ? Number(x.split(':')[0]) : 'xmas'.indexOf(x)));
return [category, '<', value, x.split(':')[1]];
} else return x;
});
}
let result = 0;
const queue = [
[
'in',
[
[1, 4000],
[1, 4000],
[1, 4000],
[1, 4000],
],
],
];
while (queue.length > 0) {
const [workflow, intervals] = queue.pop();
if (workflow == 'A' || workflow == 'R') {
if (workflow == 'A') {
result += intervals.reduce((a, b) => a * (b[1] - b[0] + 1), 1);
}
continue;
}
for (const [left, op, right, then] of workflows[workflow].slice(0, -1)) {
const [low, high] = intervals[left];
if ((op == '>' && right >= high) || (op == '<' && right <= low)) continue;
if ((op == '>' && right < low) || (op == '<' && right > high)) {
queue.push([then, intervals]);
break;
}
intervals[left] = [op == '>' ? low : right, op == '>' ? right : high];
const newIntervals = [...intervals];
newIntervals[left] = [op == '>' ? right + 1 : low, op == '>' ? high : right - 1];
queue.push([then, newIntervals]);
}
queue.push([workflows[workflow].slice(-1)[0], intervals]);
}
console.log(result);