-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.cpp
168 lines (146 loc) · 4.36 KB
/
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
#include <algorithm>
#include <exception>
#include <iostream>
#include <random>
#include <set>
#include <variant>
#include <vector>
#include "based.hpp"
#include "chacha-cpp17.hpp"
using namespace std;
rand_lib::ChaChaRng<12> global_rng(rand_lib::derive_seed(0xba5ed));
uniform_int_distribution<long long> int_dist(-1e18, 1e18);
bool all_distinct(const vector<long long> &a) {
return set(a.begin(), a.end()).size() == a.size();
}
class WrongAnswer : std::exception {
public:
WrongAnswer() = delete;
explicit WrongAnswer(const std::string &error) {
message = "this ain't it chief... " + error;
}
const char *what() const noexcept final {
return message.c_str();
}
private:
std::string message;
};
class Problem {
public:
Problem() = delete;
explicit Problem(based::Program program)
: prog(std::move(program)), correct_answer(0) {}
void run_and_check_answer(size_t max_runtime) {
prog.run(max_runtime);
if (!prog.has_output()) {
throw WrongAnswer("print something");
}
try {
auto output = prog.fetch_output<long long>();
cout << output << '\n';
if (output != correct_answer) {
throw WrongAnswer("git gud");
}
} catch (std::bad_variant_access &ex) {
throw WrongAnswer("U PRINTERD AN ENTRIE ARRAY???");
}
if (prog.has_output()) {
throw WrongAnswer("too much stuff printed");
}
}
protected:
based::Program prog;
long long correct_answer;
};
class Problem1 : public Problem {
public:
explicit Problem1(based::Program program) : Problem(std::move(program)) {
long long a = int_dist(global_rng), b = int_dist(global_rng);
prog.add_input(a);
prog.add_input(b);
correct_answer = a + b;
}
};
class Problem2 : public Problem {
public:
explicit Problem2(based::Program program) : Problem(std::move(program)) {
long long a = int_dist(global_rng);
prog.add_input(a);
correct_answer = abs(a);
}
};
class Problem3 : public Problem {
public:
Problem3(based::Program program, int n) : Problem(std::move(program)) {
vector<long long> a(n);
do {
generate(a.begin(), a.end(), [] { return int_dist(global_rng); });
} while (!all_distinct(a));
prog.add_input(n);
prog.add_input(a);
correct_answer = *max_element(a.begin(), a.end());
}
};
class Problem4 : public Problem {
public:
Problem4(based::Program program, int n) : Problem(std::move(program)) {
vector<long long> a(n);
int k = uniform_int_distribution<int>(1, n)(global_rng);
do {
generate(a.begin(), a.end(), [] { return int_dist(global_rng); });
} while (!all_distinct(a));
cout << n << ' ' << k << '\n';
for (auto i : a) cout << i << ' ';
cout << '\n';
prog.add_input(n);
prog.add_input(a);
prog.add_input(k);
nth_element(a.begin(), a.begin() + n - k, a.end());
correct_answer = a[n - k];
}
};
int main(int argc, char **argv) {
int type;
if (argc < 2) {
cout << "Usage: <checker> <n>\n";
return 1;
}
type = atoi(argv[1]);
vector<string> code;
string s;
while (getline(cin, s)) {
code.push_back(s);
}
try {
based::Program prog(code);
if (type == 1) {
for (int i = 0; i < 10; i++) {
Problem1(prog).run_and_check_answer(100000);
}
} else if (type == 2) {
for (int i = 0; i < 10; i++) {
Problem2(prog).run_and_check_answer(100000);
}
} else if (type == 3) {
for (int i = 1; i <= 50; i++) {
Problem3(prog, i).run_and_check_answer(100000);
}
} else if (type == 4) {
for (int i = 1; i <= 50; i++) {
for (int j = 0; j < (25 + i) / i; j++) {
Problem4(prog, i).run_and_check_answer(2500000);
}
}
} else {
cout << "Unknown type " << type << '\n';
return 1;
}
} catch (based::ProgramException &ex) {
cout << ex.what() << '\n';
return 1;
} catch (WrongAnswer &ex) {
cout << ex.what() << '\n';
return 1;
}
cout << "ok\n";
}