-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19b.cpp
166 lines (148 loc) · 4.6 KB
/
19b.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "lib/global.hpp"
#include "lib/os_linux.cpp"
#include "lib/format.cpp"
#include "lib/hashmap.cpp"
#include "lib/number.cpp"
#include "lib/algorithm.cpp"
int main() {
auto arr = array_load_from_file("input/19"_arr);
struct Step {
u64 property;
s8 cmp;
s64 value;
u64 next_step;
};
Array_dyn<Step> steps;
Array_dyn<s64> workflows;
array_push_back(&workflows, 0);
Hashmap<s64> id_to_workflow;
s64 index = 0;
auto read_id = [&]() {
u64 id = 0;
s64 i = 0;
while (('a' <= arr[index] and arr[index] <= 'z') or ('A' <= arr[index] and arr[index] <= 'Z')) {
assert(i < 8);
id |= (u64)arr[index] << (i*8);
++index;
++i;
}
return id;
};
auto read_int = [&]() {
u64 val = 0;
while ('0' <= arr[index] and arr[index] <= '9') {
val = 10*val + (arr[index] - '0');
++index;
}
return val;
};
auto read_char = [&](u8 c) {
if (arr[index] != c) {
format_print("%a %a\n", Array_t<u8> {&c, 1}, array_subarray(arr, index, min(index+20, arr.size)));
}
assert(arr[index] == c);
++index;
};
while (arr[index] != '\n') {
u64 workflow_id = read_id();
read_char('{');
while(arr[index] != '}') {
Step s;
s.property = read_id();
switch (arr[index]) {
case '<': ++index; s.cmp = 1; break;
case '>': ++index; s.cmp = 2; break;
default: s.cmp = 0; break;
}
if (s.cmp == 0) {
s.next_step = s.property;
s.property = 0;
} else {
s.value = read_int();
read_char(':');
s.next_step = read_id();
read_char(',');
}
array_push_back(&steps, s);
}
read_char('}');
read_char('\n');
hashmap_set(&id_to_workflow, workflow_id, workflows.size-1);
array_push_back(&workflows, steps.size);
}
read_char('\n');
struct Cube {
u64 workflow;
s64 cmin[4];
s64 cmax[4];
s64 count() {
s64 count = 1;
for (s64 i = 0; i < 4; ++i) {
if (cmin[i] >= cmax[i]) return 0;
count *= cmax[i] - cmin[i];
}
return count;
}
};
auto subcube = [&](Cube c, u64 prop, s8 cmp, s64 value) {
u8 pp;
switch (prop) {
case 'x': pp = 0; break;
case 'm': pp = 1; break;
case 'a': pp = 2; break;
case 's': pp = 3; break;
default: assert(false);
}
if (cmp == 1) {
c.cmax[pp] = min(c.cmax[pp], value);
} else if (cmp == 2) {
c.cmin[pp] = max(c.cmin[pp], value+1);
} else if (cmp == 3) {
c.cmin[pp] = max(c.cmin[pp], value);
} else if (cmp == 4) {
c.cmax[pp] = min(c.cmax[pp], value+1);
} else assert(false);
return c;
};
auto print = [](Cube c) {
return;
format_print("x[%d,%d],m[%d,%d],a[%d,%d],s[%d,%d],%a\n",
c.cmin[0], c.cmax[0],
c.cmin[1], c.cmax[1],
c.cmin[2], c.cmax[2],
c.cmin[3], c.cmax[3],
array_create_str((char*)&c.workflow));
};
Array_dyn<Cube> stack;
array_push_back(&stack, {(u64)('i' | 'n'<<8), {1, 1, 1, 1}, {4001, 4001, 4001, 4001}});
s64 sum = 0;
while (stack.size) {
Cube c = stack.back();
--stack.size;
if (c.workflow == 'A') {
sum += c.count();
continue;
} else if (c.workflow == 'R') {
continue;
}
s64 workflow_index = hashmap_get(&id_to_workflow, c.workflow);
auto ws = array_subindex(workflows, steps, workflow_index);
for (Step s: ws) {
if (s.cmp) {
Cube cc = subcube(c, s.property, s.cmp, s.value);
cc.workflow = s.next_step;
if (cc.count()) {
array_push_back(&stack, cc);
print(cc);
}
c = subcube(c, s.property, s.cmp+2, s.value);
if (c.count() == 0) break;
} else {
c.workflow = s.next_step;
array_push_back(&stack, c);
print(c);
}
}
}
format_print("%d\n", sum);
}