# To use an existing input file: # python testing-tool.py input.txt ./solution # To generate a new input file: # python testing-tool.py [N] ./solution verbose = False # Set to True to print out intermediate states import sys import subprocess import random try: if sys.argv[1][0] == "[" and sys.argv[1][-1] == "]": sys.argv[1] = sys.argv[1][1:-1] n = int(sys.argv[1]) infile = "testing-tool-input.txt" with open(infile, "w") as f: print(n, file=f) a = list(range(1, n + 1)) random.shuffle(a) print(*a, file=f) except ValueError: infile = sys.argv[1] with open(infile) as f: out = iter(subprocess.check_output(" ".join(sys.argv[2:]), shell=True, stdin=f).decode().splitlines()) inp = open(infile) n = int(next(inp)) a = list(map(int, next(inp).split())) a = [0, *a, 0] nxt = [-1] * (n + 1) prv = [-1] * (n + 1) for i in range(1, len(a)): nxt[a[i - 1]] = a[i] prv[a[i]] = a[i - 1] b = list(range(1, n + 1)) cur = nxt[0] def print_state(): x = nxt[0] for i in b: print(f"[{x}]" if cur == x else x, end=" ") x = nxt[x] if cur == 0: print("[]", end="") print() if verbose: print("The top of the stack is on the left.") print("The current position is marked with square brackets.") print() print("Initial state:") print_state() print() try: m = int(next(out)) for i in range(m): assert b c = next(out).strip() assert len(c) == 1 if c == "N": assert cur != 0 cur = nxt[cur] elif c == "P": cur = prv[cur] assert cur != 0 elif c == "R": cur = nxt[0] elif c == "M": r = cur cur = nxt[cur] nxt[prv[r]] = nxt[r] prv[nxt[r]] = prv[r] prv[r] = 0 nxt[r] = nxt[0] nxt[0] = r prv[nxt[r]] = r elif c == "T": assert cur == b[0] b.pop(0) nxt[prv[cur]] = nxt[cur] prv[nxt[cur]] = prv[cur] cur = nxt[0] else: raise ValueError() if verbose: print("Operation:", c) print_state() print() assert b == [] print("Used", m, "operations") if m >= 500000: score = 10 elif m >= 40000: score = 30 - 20 * (m - 40000) / (500000 - 40000) elif m >= 12000: score = 80 - 50 * (m - 12000) / (40000 - 12000) elif m >= 10000: score = 90 - 10 * (m - 10000) / (12000 - 10000) elif m >= 9000: score = 100 - 10 * (m - 9000) / (10000 - 9000) else: score = 100 print("Score:", score) except Exception as e: print("Output is incorrect")