6293256fec 2012-07-14 kinaba: import util; 6293256fec 2012-07-14 kinaba: import game; 6293256fec 2012-07-14 kinaba: import core.stdc.signal; 6293256fec 2012-07-14 kinaba: import std.c.stdlib; 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: abstract class Output 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: void command(char c); 6293256fec 2012-07-14 kinaba: void flush(); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: class NilOutput : Output 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: override void command(char c) {} 6293256fec 2012-07-14 kinaba: override void flush() {} 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: class StdOutput : Output 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: override void command(char c) 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: write(c); 6293256fec 2012-07-14 kinaba: stdout.flush(); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: override void flush() {} 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: // TODO: clean it up. 6293256fec 2012-07-14 kinaba: __gshared Output g_output; 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: class GuardedOutput : StdOutput 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: // Handle SIGINT: force abort and exit. 6293256fec 2012-07-14 kinaba: static this() 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: signal(SIGINT, &sigint); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: extern(C) static void sigint(int) 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: if(g_output !is null) 6293256fec 2012-07-14 kinaba: g_output.flush(); 6293256fec 2012-07-14 kinaba: else { 6293256fec 2012-07-14 kinaba: write("A"); 6293256fec 2012-07-14 kinaba: stdout.flush(); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: exit(0); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: Game g; 6293256fec 2012-07-14 kinaba: this(Game ini) { this.g = ini.clone(); ideal_log ~= g.score_if_abort_now; g_output = this; } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: string log; 6293256fec 2012-07-14 kinaba: long[] score_log; 6293256fec 2012-07-14 kinaba: long[] ideal_log; 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: override void command(char c) 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: g.command(c); 6293256fec 2012-07-14 kinaba: log ~= c; 6293256fec 2012-07-14 kinaba: score_log ~= g.score; 6293256fec 2012-07-14 kinaba: ideal_log ~= g.score_if_abort_now; 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: override void flush() 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: Tuple!(long, int, int) cand; 6293256fec 2012-07-14 kinaba: cand[0] = long.min; 6293256fec 2012-07-14 kinaba: foreach(int i, long s; score_log) 6293256fec 2012-07-14 kinaba: if(cand[0] < s) 6293256fec 2012-07-14 kinaba: cand = tuple(s,i,0); 6293256fec 2012-07-14 kinaba: foreach(int i, long s; ideal_log) 6293256fec 2012-07-14 kinaba: if(cand[0] < s) 6293256fec 2012-07-14 kinaba: cand = tuple(s,i,1); 6293256fec 2012-07-14 kinaba: if(cand[2]==0) 6293256fec 2012-07-14 kinaba: writeln(log[0..cand[1]+1]); 6293256fec 2012-07-14 kinaba: else 6293256fec 2012-07-14 kinaba: writeln(log[0..cand[1]]~"A"); 6293256fec 2012-07-14 kinaba: stdout.flush(); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: }