Compare commits

...

2 Commits

Author SHA1 Message Date
Mattéo Delabre bced251748
Fix successor test cases 2021-01-27 13:48:55 +01:00
Mattéo Delabre 349edecc4e
Parallelize 2021-01-27 13:48:34 +01:00
1 changed files with 38 additions and 22 deletions

View File

@ -1,4 +1,5 @@
from itertools import product
from multiprocessing import current_process, Pool
import signal
from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired
@ -7,6 +8,14 @@ chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'(
# Prevent zombie processes
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
# Current test case pairs used for testing (local to worker process)
current_pairs = None
def set_pairs(pairs):
global current_pairs
current_pairs = pairs
def check_pair(script, instr, outstr):
process = Popen(
@ -29,29 +38,38 @@ def check_pair(script, instr, outstr):
return False
def check_pairs(script, pairs):
for pair in pairs:
def check_script(script):
for pair in current_pairs:
if not check_pair(script, *pair):
return False
return True
return script, False
return script, True
def generate_scripts(max_length):
for length in range(max_length + 1):
for letters in product(chars, repeat=length):
yield "".join(letters)
def find_script(pairs, max_length):
candidates = []
chars_count = len(chars)
total_options = int((chars_count ** (max_length + 1) - 1) / (chars_count - 1))
current_option = 0
num_tasks = int((chars_count ** (max_length + 1) - 1) / (chars_count - 1))
done_tasks = 0
for length in range(max_length + 1):
for letters in product(chars, repeat=length):
current_option += 1
with Pool(processes=8, initializer=set_pairs, initargs=(pairs,)) as pool:
for script, result in pool.imap_unordered(
check_script,
generate_scripts(max_length),
chunksize=10,
):
done_tasks += 1
if current_option % 1000 == 0:
print(f"Progress: {current_option}/{total_options}")
if done_tasks % 10000 == 0:
print(f"Progress: {done_tasks}/{num_tasks}")
script = "".join(letters)
if check_pairs(script, pairs):
if result:
print("> Found candidate:", script)
candidates.append(script)
@ -59,7 +77,6 @@ def find_script(pairs, max_length):
if __name__ == '__main__':
# Identity
print("\nSearching for identity")
find_script((
("1", "1"),
@ -67,10 +84,9 @@ if __name__ == '__main__':
("1984", "1984"),
), max_length=3)
# Successor
# print("\nSearching for successor")
# find_script((
# ("1", "2"),
# ("2", "3"),
# ("3", "4"),
# ), max_length=5)
print("\nSearching for successor")
find_script((
("1", "2"),
("42", "43"),
("1984", "1985"),
), max_length=5)