commit bc244e3a795a0f9daf06754ec39d6a171976b9af Author: Mattéo Delabre Date: Tue Jan 26 22:36:26 2021 +0100 Initial commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fa4860a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM alpine +RUN apk add --no-cache bash python3 +RUN adduser -D user +USER user +ADD runall.py / +CMD ["python3", "/runall.py"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..09dd265 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +How to run: + +``` +docker image build --quiet --tag runall-image . +docker container run --tty --rm --read-only runall-image +``` diff --git a/runall.py b/runall.py new file mode 100644 index 0000000..9d19ae1 --- /dev/null +++ b/runall.py @@ -0,0 +1,58 @@ +from itertools import product +import string +import subprocess + + +def check_pair(script, instr, outstr): + result = subprocess.run( + ["/usr/bin/env", "bash", "-c", script], + input=instr.encode(), + capture_output=True, + ) + return result.returncode == 0 and result.stdout == outstr.encode() + + +def check_pairs(script, pairs): + for pair in pairs: + if not check_pair(script, *pair): + return False + return True + + +def find_script(pairs, max_length): + candidates = [] + total_options = len(string.printable) ** max_length + current_option = 0 + + for length in range(max_length + 1): + for letters in product(string.printable, repeat=length): + if current_option % 1000 == 0: + print(f"Progress: {current_option}/{total_options}") + + script = "".join(letters) + + if check_pairs(script, pairs): + print("> Found candidate:", script) + candidates.append(script) + + current_option += 1 + + print("Candidates:", candidates) + + +if __name__ == '__main__': + # Identity + print("\nSearching for identity") + find_script(( + ("1", "1"), + ("2", "2"), + ("3", "3"), + ), max_length=2) + + # Successor + print("\nSearching for successor") + find_script(( + ("1", "2"), + ("2", "3"), + ("3", "4"), + ), max_length=5)