-
Notifications
You must be signed in to change notification settings - Fork 0
/
icfplib.d
269 lines (238 loc) · 4.41 KB
/
icfplib.d
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
module icfputil.icfplib;
import std.algorithm;
import std.json;
import std.range;
import std.stdio;
import std.string;
import icfputil.common;
immutable string address = "icfpc2013.cloudapp.net";
immutable string salt = "vpsH1H";
immutable string data_path = "data/";
immutable string id_file = data_path ~ "id.txt";
immutable string problems_file = data_path ~ "problems.txt";
string auth;
void init ()
{
auto f_in = File (id_file, "rt");
auth = f_in.readln ().strip ();
auth ~= salt;
}
struct Problem
{
static immutable double TIME_FREE = 300.0 + 1.0;
string id;
long size;
string [] operators;
bool solved = false;
double timeLeft = TIME_FREE;
this (this)
{
operators = operators.dup;
}
this (string new_id, long new_size, string [] new_operators)
{
id = new_id;
size = new_size;
operators = new_operators.dup;
}
this (TrainingProblem tp)
{
this (tp.id, tp.size, tp.operators);
}
this (JSONValue value)
{
auto obj = value.object;
this (obj["id"].str,
obj["size"].integer,
obj["operators"].array.map !(x => x.str).array);
if ("solved" in obj)
{
solved = obj["solved"].type == JSON_TYPE.TRUE;
}
if ("timeLeft" in obj)
{
timeLeft = obj["timeLeft"].floating;
}
}
}
void write_problems_sorted (alias fun) (string f_name, Problem [] problems)
{
auto f_out = File (f_name, "wt");
auto cur = problems.filter !(x => !x.solved && x.timeLeft >= 0).array;
cur.sort !(fun);
f_out.writefln ("%(%s\n%)", cur);
}
JSONValue JSONObject (JSONValue [string] aa)
{
JSONValue res;
res.type = JSON_TYPE.OBJECT;
foreach (key, value; aa)
{
res.object[key] = value;
}
return res;
}
JSONValue JSONArray (JSONValue [] arr)
{
JSONValue res;
res.type = JSON_TYPE.ARRAY;
foreach (value; arr)
{
res.array ~= value;
}
return res;
}
JSONValue JSONString (string value)
{
JSONValue res;
res.type = JSON_TYPE.STRING;
res.str = value;
return res;
}
JSONValue JSONInteger (long value)
{
JSONValue res;
res.type = JSON_TYPE.INTEGER;
res.integer = value;
return res;
}
struct EvalRequest
{
string id;
string program;
string [] arguments;
this (this)
{
arguments = arguments.dup;
}
JSONValue toJSONValue ()
{
JSONValue [string] aa;
if (id.length > 0)
{
aa["id"] = id.JSONString;
}
if (program.length > 0)
{
aa["program"] = program.JSONString;
}
aa["arguments"] = arguments.map !(x => JSONString (x))
.array.JSONArray;
return JSONObject (aa);
}
}
struct EvalResponse
{
string status;
string [] outputs;
string message;
this (this)
{
outputs = outputs.dup;
}
this (JSONValue value)
{
auto obj = value.object;
status = obj["status"].str;
if ("outputs" in obj)
{
outputs = obj["outputs"].array
.map !(x => x.str)
.array;
}
if ("message" in obj)
{
message = obj["message"].str;
}
}
}
struct Guess
{
string id;
string program;
JSONValue toJSONValue ()
{
JSONValue [string] aa;
aa["id"] = id.JSONString;
aa["program"] = program.JSONString;
return JSONObject (aa);
}
}
struct GuessResponse
{
string status;
string [] values;
string message;
bool lightning;
this (this)
{
values = values.dup;
}
this (JSONValue value)
{
auto obj = value.object;
status = obj["status"].str;
if ("values" in obj)
{
values = obj["values"].array
.map !(x => x.str)
.array;
}
if ("message" in obj)
{
message = obj["message"].str;
}
if ("lightning" in obj)
{
lightning = obj["lightning"].type == JSON_TYPE.TRUE;
}
}
}
struct TrainRequest
{
long size;
string [] operators;
this (this)
{
operators = operators.dup;
}
JSONValue toJSONValue ()
{
JSONValue [string] aa;
if (size != NA)
{
aa["size"] = size.JSONInteger;
}
if (operators == [""])
{
aa["operators"] = [].JSONArray;
}
else if (operators.length > 0)
{
aa["operators"] = operators.map !(x => JSONString (x))
.array.JSONArray;
}
return JSONObject (aa);
}
}
struct TrainingProblem
{
string challenge;
string id;
long size;
string [] operators;
this (this)
{
operators = operators.dup;
}
this (JSONValue value)
{
auto obj = value.object;
challenge = obj["challenge"].str;
id = obj["id"].str;
size = obj["size"].integer;
operators = obj["operators"].array
.map !(x => x.str)
.array;
}
}