import argparse import os import random 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'] basedir = os.path.abspath(os.path.dirname(__file__)) solution_filepath = f'{basedir}/a.txt' if os.path.exists(solution_filepath): os.remove(solution_filepath) seed = random.randint(0, 2147483647) # seed: uint32_t <= 2147483647 cmds = f'{basedir}/solver_soft/sim -o {solution_filepath} -s {seed}'.split() proc = subprocess.Popen( cmds, stdin=subprocess.PIPE, # stderr=subprocess.PIPE, # stdout=subprocess.PIPE ) try: proc.communicate(problem.encode()) status = 'done' except Exception as ex: status = 'failed' finally: returncode = proc.returncode solution = '' if returncode == 0: status = 'done' if os.path.exists(solution_filepath): with open(solution_filepath, 'r') as fp: solution = fp.read() else: status = 'failed' else: status = 'failed' return { 'status': status, 'solution': solution } def main(args): with open(args['problem'], 'r') as fp: problem = fp.read() params = { 'problem': problem, 'timeout': args['timeout'] } 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)