forked from theodormoroianu/mateinfoUB-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMS_checker.cpp
285 lines (232 loc) · 6.35 KB
/
CMS_checker.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
/**
* Author: Alexandru Tifui, Theodor Moroianu
*/
#include <bits/stdc++.h>
using namespace std;
// Global state of the interpreter.
int N;
vector<string> instructions;
vector<vector<string>> decoded_ops;
vector<int> reg(5, 0);
vector<int> perm;
int no_executed_instructions = 0;
const int NO_EXEC_INSTR_THRESHOLD = 5'000'000;
const int NO_MAX_INSTR_THRESHOLD = 1'000;
bool DISPLAY_MOVES = false;
vector<string> DecodeInstruction(string instr) {
vector<string> tokens;
tokens.push_back("");
for (auto i : instr) {
if (i == ' ') {
tokens.push_back("");
} else {
tokens.back().push_back(i);
}
}
// removing the index of the instruction
tokens.erase(tokens.begin());
while (!tokens.empty() and tokens.back() == "")
tokens.pop_back();
return tokens;
}
bool IsVariableRegister(string s) {
return s == "A" or s == "B" or s == "C" or s == "D" or s == "E";
}
bool IsConstantRegister(string s) { return s == "N" or s == "Z"; }
bool IsRegister(string s) {
return IsVariableRegister(s) or IsConstantRegister(s);
}
int GetRegisterValue(string s) {
assert(IsRegister(s));
if (IsVariableRegister(s))
return reg[s[0] - 'A'];
if (s == "N")
return N;
if (s == "Z")
return 0;
assert(false);
}
bool FetchAndDecode(string instr, int expected_instruction_no) {
int computed_instruction_no = 0;
int pos = 0;
int len = instr.size();
while (pos < len and isdigit(instr[pos])) {
computed_instruction_no = computed_instruction_no * 10 + instr[pos] - '0';
pos++;
}
if (computed_instruction_no != expected_instruction_no)
return false;
if (instr[pos] != '.')
return false;
pos++;
if (instr[pos] != ' ')
return false;
pos++;
vector<string> instruction = DecodeInstruction(instr);
decoded_ops.push_back(instruction);
if (instruction.size() < 1)
return false;
if (instruction[0] == "IF_LESS_GOTO" or instruction[0] == "IF_SAME_GOTO" or
instruction[0] == "IF_DIFF_GOTO") {
if (instruction.size() != 4)
return false;
if (!IsRegister(instruction[1]))
return false;
if (!IsRegister(instruction[2]))
return false;
if (stoi(instruction[3]) < 0 or
stoi(instruction[3]) >= (int)instructions.size())
return false;
} else if (instruction[0] == "ASSIGN") {
if (instruction.size() != 3)
return false;
if (!IsVariableRegister(instruction[1]))
return false;
if (!IsRegister(instruction[2]))
return false;
} else if (instruction[0] == "INC" or instruction[0] == "DEC" or
instruction[0] == "PLOAD") {
if (instruction.size() != 2)
return false;
if (!IsVariableRegister(instruction[1]))
return false;
} else if (instruction[0] == "SWAP" or instruction[0] == "PSWAP") {
if (instruction.size() != 3)
return false;
if (!IsVariableRegister(instruction[1]))
return false;
if (!IsVariableRegister(instruction[2]))
return false;
} else if (instruction[0] == "END") {
if (instruction.size() != 1)
return false;
} else
return false;
return true;
}
bool LoadAndValidateInstructions(ifstream &fout) {
string line;
while (getline(fout, line)) {
instructions.push_back(line);
}
if (instructions.size() > NO_MAX_INSTR_THRESHOLD) {
return false;
}
int idx = 0;
for (auto instr : instructions) {
if (!FetchAndDecode(instr, idx)) {
return false;
}
idx++;
}
return true;
}
void ExecuteInstruction(vector<string> I, int &pc) {
// do pc-- after the jump in case of GOTO instructions
if (I[0] == "IF_LESS_GOTO") {
int val1 = GetRegisterValue(I[1]);
int val2 = GetRegisterValue(I[2]);
if (val1 < val2) {
pc = stoi(I[3]);
pc--;
}
} else if (I[0] == "IF_DIFF_GOTO") {
int val1 = GetRegisterValue(I[1]);
int val2 = GetRegisterValue(I[2]);
if (val1 != val2) {
pc = stoi(I[3]);
pc--;
}
} else if (I[0] == "IF_SAME_GOTO") {
int val1 = GetRegisterValue(I[1]);
int val2 = GetRegisterValue(I[2]);
if (val1 == val2) {
pc = stoi(I[3]);
pc--;
}
} else if (I[0] == "ASSIGN") {
int val2 = GetRegisterValue(I[2]);
reg[I[1][0] - 'A'] = val2;
} else if (I[0] == "INC") {
reg[I[1][0] - 'A']++;
} else if (I[0] == "DEC") {
reg[I[1][0] - 'A']--;
} else if (I[0] == "PLOAD") {
int val = GetRegisterValue(I[1]);
if (val < 0 or val >= N)
throw "Runtime error";
reg[I[1][0] - 'A'] = perm[val];
} else if (I[0] == "SWAP") {
int val1 = GetRegisterValue(I[1]);
int val2 = GetRegisterValue(I[2]);
reg[I[1][0] - 'A'] = val2;
reg[I[2][0] - 'A'] = val1;
} else if (I[0] == "PSWAP") {
int val1 = GetRegisterValue(I[1]);
if (val1 < 0 or val1 >= N)
throw "Runtime error";
int val2 = GetRegisterValue(I[2]);
if (val2 < 0 or val2 >= N)
throw "Runtime error";
swap(perm[val1], perm[val2]);
} else if (I[0] == "END") {
pc = -2;
} else
throw "Runtime error";
}
void Execute() {
for (int pc = 0; pc < (int)decoded_ops.size(); pc++) {
ExecuteInstruction(decoded_ops[pc], pc);
no_executed_instructions++;
if (no_executed_instructions > NO_EXEC_INSTR_THRESHOLD) {
throw "Too many executed instructions\n";
}
if (pc == -2) // END was called
break;
}
}
bool valid_output() {
for (int i = 0; i < N; i++)
if (perm[i] != i)
return false;
return true;
}
int main(int argc, char **argv) {
assert(argc == 4);
string input_file(argv[1]);
string output_file(argv[3]);
string answer_file(argv[2]);
ifstream fin(input_file);
ifstream fout(output_file);
ifstream fok(answer_file);
// load_ok_data(fin, fok);
fin >> N;
perm.resize(N);
try {
if (!LoadAndValidateInstructions(fout)) {
throw "Invalid input\n";
}
// try 10 random permutations of size N
mt19937 rnd(42);
for (int trial = 0; trial < 10; trial++) {
iota(perm.begin(), perm.end(), 0);
shuffle(perm.begin(), perm.end(), rnd);
// reset all registers
fill(reg.begin(), reg.end(), 0);
no_executed_instructions = 0;
auto original_permutation = perm;
Execute();
if (!valid_output()) {
cerr << "translate:wrong\n";
cout << "0.0\n";
return 0;
}
}
cout << "1.0\n";
cerr << "translate:success\n";
} catch (const char *e) {
cerr << "translate:wrong\n";
cout << "0.0\n";
}
return 0;
}