Write valid scripts instead of invalid ones

This commit is contained in:
Mattéo Delabre 2021-01-27 19:51:11 +01:00
parent a653a90ad1
commit a1baf3ad3f
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
2 changed files with 16 additions and 15 deletions

View File

@ -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)

6
run.py
View File

@ -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)