From c79e76603400980611e7d41e17e78512a23291f5 Mon Sep 17 00:00:00 2001 From: Kento HASEGAWA Date: Fri, 23 Aug 2019 13:32:08 +0900 Subject: [PATCH] Add a script for ADC2019 system --- iskw_solver.py | 108 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 iskw_solver.py diff --git a/iskw_solver.py b/iskw_solver.py new file mode 100644 index 0000000..6eb36fe --- /dev/null +++ b/iskw_solver.py @@ -0,0 +1,108 @@ +import argparse +import os +import subprocess + +proc = None +stop_flag = False + +def stop(): + global proc + global stop_flag + + stop_flag = True + if not proc is None: + proc.terminate() + print('stopped') + +def solve(params): + global proc + global stop_flag + + stop_flag = False + + # 必要な変数があるか確認 + assert('problem' in params) + + problem = params['problem'] + + pl = problem.splitlines() + for l in pl: + if "SIZE" in l: + board_size_str = l.strip().split()[1] + bx, by = [int(v) for v in board_size_str.split('X')] + break + + basedir = os.path.abspath(os.path.dirname(__file__)) + + cmds = f'{basedir}/main'.split() + + proc = subprocess.Popen( + cmds, + stdin=subprocess.PIPE, + stderr=subprocess.PIPE, + stdout=subprocess.PIPE + ) + + try: + outs, errs = proc.communicate(problem.encode()) + status = 'done' + except Exception as ex: + status = 'failed' + finally: + returncode = proc.returncode + outs = outs.decode() + errs = errs.decode() + print(outs) + print(errs) + + solution = '' + if returncode == 0: # 10 when SAT + status = 'done' + + lines = outs.splitlines() + state = 0 + for l in lines: + + if "SIZE" in l: + board_size_str = l.strip().split()[1] + sx, sy = [int(v) for v in board_size_str.split('X')] + if sx > by or sy > by: + status = 'failed' + break + elif 'ANSWER' in l: + state = 1 + continue + elif state == 1: + if l.strip() == '': + break + else: + solution += l + '\n' + else: + status = 'failed' + + return { + 'status': status, + 'solution': solution + } + +def main(args): + + with open(args['problem'], 'r') as fp: + problem = fp.read() + + params = { + 'problem': problem, + } + + ret = solve(params) + + print(ret) + +if __name__ == "__main__": + + parser = argparse.ArgumentParser(description='Wrapper Script for ADC2019 Solver') + parser.add_argument('problem', type=str, help='File path to the problem file') + parser.add_argument('-t', '--timeout', type=int, default=10, help='Timeout for the process of the solver') + args = vars(parser.parse_args()) + + main(args) -- 2.22.0