高専プロコンの練習用のAPI叩くアレ
module kpcp.kpcp;
import std.net.curl;
import std.conv;
import std.stdio;
import std.uri;
import std.parallelism;
import std.json;
import std.array;
import std.format;
import std.exception;
enum baseURL = `http://procon2014-practice.oknct-ict.org`;
typeof(func("url")) getProblem(alias func)(uint id)
if(is(typeof(func("url"))))
{
return func(baseURL ~ `/problem/ppm/` ~ id.to!string);
}
alias getProblemAsBinary = getProblem!(get!(AutoProtocol, ubyte));
alias getProblemAsBinaryTask = getProblem!(a => task!(get!(AutoProtocol, ubyte))(a));
alias getProblemAsChunkAsync = getProblem!(a => byChunkAsync(a));
struct AnswerResult
{
bool isAccepted;
ulong mismatchCells;
ulong swapScore;
ulong selectScore;
ulong totalScore;
}
AnswerResult sendAnswer(uint id, string userName, string password, string answer)
{
auto app = appender!string();
app.formattedWrite("username=%s", userName.encode);
app.formattedWrite("&passwd=%s", password.encode);
app.formattedWrite("&answer_text=%s", answer.encode);
auto jsonData = post(baseURL ~ "/solve/json/" ~ id.to!string, app.data).parseJSON();
AnswerResult result;
result.isAccepted = jsonData["status"].str == "Accept";
ulong getUlong(JSONValue jv)
{
if(jv.type == JSON_TYPE.UINTEGER)
return jv.uinteger;
else if(jv.type == JSON_TYPE.INTEGER)
return jv.integer.to!ulong();
else
enforce(0);
assert(0);
}
result.mismatchCells = getUlong(jsonData["mismatch_cells"]);
result.swapScore = getUlong(jsonData["swap_score"]);
result.selectScore = getUlong(jsonData["select_score"]);
result.totalScore = getUlong(jsonData["total_score"]);
return result;
}
void main()
{
auto p = getProblemAsBinary(1);
writeln(p[0 .. 4].to!string);
auto r = sendAnswer(1, "testkpcp", "1234", "2\r\n11\r\n21\r\nURDDLLURRDLLUURDDLUUD\r\n11\r\n40\r\nURDLURLDLURDRDLURUDLURDLLRDLUURRDLLURRDL\r\n");
writeln(r);
}