Compare commits

..

No commits in common. "bced2517481216c08c1aa95eea97e69a266142a6" and "cd6197caa33e9356e97518eaf54a5cfa592670da" have entirely different histories.

1 changed files with 22 additions and 38 deletions

View File

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