-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.cpp
330 lines (299 loc) · 10.8 KB
/
day24.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "utils/helpers.hpp"
#define UNKNOWN 0
#define XOR 1
#define OR 2
#define AND 3
#define LEAF 4
typedef std::map<int, std::vector<int>> OpCollector;
std::string opToStr(int op) {
switch (op)
{
case XOR:
return " ^ ";
break;
case AND:
return " & ";
break;
case OR:
return " | ";
break;
default:
return " = ";
break;
}
}
class Node {
public:
std::string name;
std::string left;
std::string right;
int op;
bool has_val;
bool val;
Node(): op(0) {}
Node(const std::string& n, const std::string& l, const std::string& r, int o) :
name(n), left(l), right(r), op(o), has_val(false), val(false) {}
Node(const std::string& n, int v) :
name(n), left(""), right(""), op(LEAF), has_val(true), val(v) {}
Node(const Node& o) :
name(o.name), left(o.left), right(o.right), op(o.op), has_val(o.has_val), val(o.val) {}
bool evaluate(std::map<std::string, Node>& np) {
bool leftVal;
bool rightVal;
if (has_val)
return val;
leftVal = np[left].evaluate(np);
rightVal = np[right].evaluate(np);
switch (op)
{
case XOR:
val = leftVal ^ rightVal;
break;
case AND:
val = leftVal & rightVal;
break;
case OR:
val = leftVal | rightVal;
break;
default:
std::cout << "unknown operand\n";
break;
}
has_val = true;
return val;
}
void getOpRecu(std::map<std::string, Node>& np, int depth, OpCollector& col) {
col[depth].push_back(op);
if (op != LEAF) {
np[left].getOpRecu(np, depth + 1, col);
np[right].getOpRecu(np, depth + 1, col);
}
}
bool checkFirstTruc(std::map<std::string, Node>& np) {
if (name == "z00" || name == "z01" || name == "z05" || name == "z06" || name == "z09"
|| name == "z10" || name == "z15" || name == "z16" || name == "zd30")
return true;
if (op == XOR) {
Node x;
Node o;
if (np[left].op == OR)
o = np[left];
else if (np[left].op == XOR)
x = np[left];
if (np[right].op == OR)
o = np[right];
else if (np[right].op == XOR)
x = np[right];
if (x.op == UNKNOWN || o.op == UNKNOWN) {
std::cout << name << " bad operands on " << right << " or " << left << '\n';
return false;
}
if (!x.isALeafPairXor(np))
return false;
if (!o.isARemain(np))
return false;
return true;
}
std::cout << name << " had to have operand ^ but has" << opToStr(op) << '\n';
return false;
}
bool isARemain(std::map<std::string, Node>& np) {
Node pouet;
if (op != OR)
{
std::cout << name << " his operand had to be a OR but is a" << opToStr(op) << '\n';
return false;
}
if (np[right].op != AND && np[left].op != AND)
{
std::cout << name << " has to have left and eight with operand right " << right << " has op = ";
std::cout << opToStr(np[right].op) << "and left " << left << " has op =" << opToStr(np[left].op) << '\n';
return false;
}
if (np[left].isALeafPairAnd(np)) {
pouet = np[right];
}
else if (np[right].isALeafPairAnd(np)) {
pouet = np[left];
}
else {
std::cout << name << " had to have a leaf pair with & operand but has not\n";
return false;
}
if (np[pouet.left].isALeafPairXor(np) || np[pouet.right].isALeafPairXor(np)) {
return true;
}
std::cout << name << " no leaf pair xor child for pouet\n";
return false;
}
bool isALeafPairAnd(std::map<std::string, Node>& np) {
if (op != AND)
{
std::cout << name << " had to have operand & but has" << opToStr(op) << '\n';
return false;
}
if (np[right].op != LEAF) {
std::cout << name << " has right member " << right << " which had to be a LEAF.\n";
return false;
}
if (np[left].op != LEAF) {
std::cout << name << " has left member " << left << " which had to be a LEAF.\n";
return false;
}
return true;
}
bool isALeafPairXor(std::map<std::string, Node>& np) {
if (op != XOR)
{
// std::cout << name << " had to have operand ^ but has " << opToStr(op) << '\n';
return false;
}
if (np[right].op != LEAF) {
// std::cout << name << " has right member " << right << " which had to be a LEAF.\n";
return false;
}
if (np[left].op != LEAF) {
// std::cout << name << " has left member " << left << " which had to be a LEAF.\n";
return false;
}
return true;
}
bool isAZ(std::map<std::string, Node>& np) {
if (op == XOR) {
if (np[left].op == XOR || np[right].op == XOR)
return true;
}
return false;
}
};
typedef std::map<std::string, Node> NodeMap;
void printCol(const OpCollector& op) {
for (OpCollector::const_iterator it2 = op.begin(); it2 != op.end(); ++it2) {
std::cout << it2->first << "-> ";
for (int op: it2->second) {
std::cout << opToStr(op) << ' ';
}
std::cout << '\n';
}
}
int getOperand(std::string op) {
if (op == "XOR")
return XOR;
if (op == "AND")
return AND;
return OR;
}
int main() {
ui part;
std::ifstream input;
std::string line("init");
std::vector<std::string> splitted;
ull resP1 = 0;
ull exp = 0;
NodeMap np;
std::set<std::string> resP2;
if (getFileAndPart(24, input, part))
return errno;
while (!line.empty()) {
getline(input, line);
inputLib::carriageReturnDel(line);
np[line.substr(0, 3)] = Node(line.substr(0, 3), line[5] == '1' ? true : false);
}
while (!input.eof()) {
getline(input, line);
inputLib::carriageReturnDel(line);
splitted = inputLib::split(line, " ");
np[splitted[4]] = Node(splitted[4], splitted[0], splitted[2], getOperand(splitted[1]));
}
if (part == 1) {
for (NodeMap::iterator it = np.begin(); it != np.end(); ++it) {
if (it->first[0] == 'z') {
it->second.evaluate(np);
if (it->second.val)
resP1 += ullPow(2, exp);
++exp;
}
}
std::cout << "result is " << resP1 << '\n';
}
else {
OpCollector col;
for (NodeMap::iterator it = np.begin(); it != np.end(); ++it) {
// if (it->first[0] != 'z') {
// if (it->second.isAZ(np)) {
// col.clear();
// std::cout << it->first << '\n';
// it->second.getOpRecu(np, 0, col);
// printCol(col);
// }
// }
if (it->first[0] == 'z') {
if (!(it->second.checkFirstTruc(np))) {
col.clear();
it->second.getOpRecu(np, 0, col);
std::cout << it->first << '\n';
for (OpCollector::iterator it2 = col.begin(); it2 != col.end(); ++it2) {
std::cout << it2->first << "-> ";
for (int op: it2->second) {
std::cout << opToStr(op) << ' ';
}
std::cout << '\n';
}
break;
}
// if (it->second.op != XOR) {
// std::cout << it->first << " has wrong operator\n";
// resP2.insert(it->first);
// }
// else {
// int opL = np[it->second.left].op;
// int opR = np[it->second.right].op;
// // std::cout << opToStr(np[it->second.left].op) << " - " << opToStr(np[it->second.right].op) << '\n';
// if (opL == AND || opR == AND) {
// if (opL == AND) {
// std::cout << it->second.left << " has wrong operator\n";
// resP2.insert(it->second.left);
// }
// else {
// std::cout << it->second.right << " has wrong operator\n";
// resP2.insert(it->second.right);
// }
// }
// else {
// NodeMap::const_iterator itR = np.find(it->second.right);
// NodeMap::const_iterator itL = np.find(it->second.left);
// if (opR == OR) {
// // std::cout << opToStr(np[itR->second.left].op) << " - " << opToStr(np[itR->second.right].op) << '\n';
// if (np[itR->second.left].op != AND) {
// std::cout << itR->second.left << " has wrong operator \n";
// resP2.insert(itR->second.left);
// }
// if (np[itR->second.right].op != AND) {
// std::cout << itR->second.right << " has wrong operator \n";
// resP2.insert(itR->second.right);
// }
// }
// else if (opL == OR) {
// // std::cout << opToStr(np[itL->second.left].op) << " - " << opToStr(np[itL->second.right].op) << '\n';
// if (np[itL->second.left].op != AND) {
// std::cout << itL->second.left << " has wrong operator \n";
// resP2.insert(itL->second.left);
// }
// if (np[itL->second.right].op != AND) {
// std::cout << itL->second.right << " has wrong operator \n";
// resP2.insert(itL->second.right);
// }
// }
// }
// }
}
}
for (const std::string& elt : resP2) {
std::cout << elt << ',';
}
std::cout << '\n';
std::cout << "not jfb,jgt,mht,nbf,z05,z09,z30,z45\n";
std::cout << "not gbf,jfb,jgt,mht,nbf,z05,z09,z30\n";
}
return 0;
}