From a1baf3ad3fd45c7598d981205454c635c25ead7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Wed, 27 Jan 2021 19:51:11 +0100 Subject: [PATCH] Write valid scripts instead of invalid ones --- autogolf/__init__.py | 25 +++++++++++++------------ run.py | 6 +++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/autogolf/__init__.py b/autogolf/__init__.py index f94bf71..bdf0630 100644 --- a/autogolf/__init__.py +++ b/autogolf/__init__.py @@ -11,10 +11,10 @@ chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'( class Status(Enum): """Possible correctness statuses for a program.""" - Correct = auto() - WrongAnswer = auto() - Timeout = auto() - Invalid = auto() + Correct = auto() # returns the expected output and a 0 exit code + Invalid = auto() # has a syntax error or uses an undefined command + WrongAnswer = auto() # returns an unknown exit code or invalid output + Timeout = auto() # ran for more time than allowed by the timeout value def check_pair(script, instr, outstr, timeout) -> Status: @@ -91,7 +91,7 @@ def find_script( max_length, processes, timeout, - invalid_prefix, + out_valid_prefix, out_log, ) -> List[str]: """ @@ -101,17 +101,18 @@ def find_script( :param max_length: maximum script length to test :param processes: number of parallel processes to spawn :param timeout: maximum allowed time in seconds for each script run - :param invalid_prefix: prefix to the files in which invalid scripts - are to be stored, one file per script length + :param out_valid_prefix: store valid scripts (includes correct scripts + and incorrect scripts that do not contain syntax or runtime errors) + into files starting with this prefix :param out_log: stream to which progress logs are written :returns: list of matching scripts """ candidates = [] - out_invalid = [] + out_valid = [] bound_check_script = partial(check_script, pairs, timeout) for i in range(max_length + 1): - out_invalid.append(open(invalid_prefix + str(i), "w")) + out_valid.append(open(out_valid_prefix + str(i), "w")) chars_count = len(chars) num_tasks = int((chars_count ** (max_length + 1) - 1) / (chars_count - 1)) @@ -141,10 +142,10 @@ def find_script( ) candidates.append(script) - if status == Status.Invalid: - print(script, file=out_invalid[len(script)]) + if status != Status.Invalid: + print(script, file=out_valid[len(script)], flush=True) - for file in out_invalid: + for file in out_valid: file.close() print(f"Finished in {time() - start_time:.1f}s", file=out_log, flush=True) diff --git a/run.py b/run.py index 4be2b4f..e02530b 100644 --- a/run.py +++ b/run.py @@ -3,7 +3,7 @@ import sys processes = 8 timeout = 5 # seconds -invalid_prefix = "output/invalid_scripts_" +out_valid_prefix = "output/valid_scripts_" out_log = sys.stdout print("\nSearching for identity") @@ -16,7 +16,7 @@ identity = autogolf.find_script( max_length=3, processes=processes, timeout=timeout, - invalid_prefix=invalid_prefix, + out_valid_prefix=out_valid_prefix, out_log=out_log, ) print("Candidates:", identity) @@ -31,7 +31,7 @@ successor = autogolf.find_script( max_length=5, processes=processes, timeout=timeout, - invalid_prefix=invalid_prefix, + out_valid_prefix=out_valid_prefix, out_log=out_log, ) print("Candidates:", successor)