Compare commits
2 Commits
cd6197caa3
...
bced251748
Author | SHA1 | Date |
---|---|---|
Mattéo Delabre | bced251748 | |
Mattéo Delabre | 349edecc4e |
60
runall.py
60
runall.py
|
@ -1,4 +1,5 @@
|
||||||
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
|
||||||
|
|
||||||
|
@ -7,6 +8,14 @@ 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(
|
||||||
|
@ -29,29 +38,38 @@ def check_pair(script, instr, outstr):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def check_pairs(script, pairs):
|
def check_script(script):
|
||||||
for pair in pairs:
|
for pair in current_pairs:
|
||||||
if not check_pair(script, *pair):
|
if not check_pair(script, *pair):
|
||||||
return False
|
return script, False
|
||||||
return True
|
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):
|
def find_script(pairs, max_length):
|
||||||
candidates = []
|
candidates = []
|
||||||
|
|
||||||
chars_count = len(chars)
|
chars_count = len(chars)
|
||||||
total_options = int((chars_count ** (max_length + 1) - 1) / (chars_count - 1))
|
num_tasks = int((chars_count ** (max_length + 1) - 1) / (chars_count - 1))
|
||||||
current_option = 0
|
done_tasks = 0
|
||||||
|
|
||||||
for length in range(max_length + 1):
|
with Pool(processes=8, initializer=set_pairs, initargs=(pairs,)) as pool:
|
||||||
for letters in product(chars, repeat=length):
|
for script, result in pool.imap_unordered(
|
||||||
current_option += 1
|
check_script,
|
||||||
|
generate_scripts(max_length),
|
||||||
|
chunksize=10,
|
||||||
|
):
|
||||||
|
done_tasks += 1
|
||||||
|
|
||||||
if current_option % 1000 == 0:
|
if done_tasks % 10000 == 0:
|
||||||
print(f"Progress: {current_option}/{total_options}")
|
print(f"Progress: {done_tasks}/{num_tasks}")
|
||||||
|
|
||||||
script = "".join(letters)
|
if result:
|
||||||
|
|
||||||
if check_pairs(script, pairs):
|
|
||||||
print("> Found candidate:", script)
|
print("> Found candidate:", script)
|
||||||
candidates.append(script)
|
candidates.append(script)
|
||||||
|
|
||||||
|
@ -59,7 +77,6 @@ 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"),
|
||||||
|
@ -67,10 +84,9 @@ if __name__ == '__main__':
|
||||||
("1984", "1984"),
|
("1984", "1984"),
|
||||||
), max_length=3)
|
), max_length=3)
|
||||||
|
|
||||||
# Successor
|
print("\nSearching for successor")
|
||||||
# print("\nSearching for successor")
|
find_script((
|
||||||
# find_script((
|
("1", "2"),
|
||||||
# ("1", "2"),
|
("42", "43"),
|
||||||
# ("2", "3"),
|
("1984", "1985"),
|
||||||
# ("3", "4"),
|
), max_length=5)
|
||||||
# ), max_length=5)
|
|
||||||
|
|
Loading…
Reference in New Issue