Advertisement
pb_jiang

gen3.py

Jun 15th, 2023
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3. import random
  4. import subprocess
  5.  
  6.  
  7. def main():
  8.     n = random.randint(1, 1e6)
  9.     sys.stderr.write(f"begin with {n=}\n")
  10.     v = [x + 1 for x in range(n)]
  11.     random.shuffle(v)
  12.     idx = 0
  13.     opt = 0
  14.     process = subprocess.Popen(["./g2.exe"],
  15.                                stdin=subprocess.PIPE,
  16.                                stdout=subprocess.PIPE)
  17.     process.stdin.write(bytes(f"{v[idx]}\n", "utf8")), process.stdin.flush()
  18.     while process.poll() is None:
  19.         sys.stdin.flush()
  20.         line = process.stdout.readline().strip()
  21.         #sys.stderr.write(f"read {line}\n")
  22.         if line[0] == ord('!'):
  23.             exp = int(line[1:])
  24.             if exp == n:
  25.                 sys.stderr.write(f"get right ans {exp=}\n")
  26.                 return 0
  27.             else:
  28.                 raise Exception(f"real {n=}, {exp=}\n")
  29.         else:
  30.             op = line[0]
  31.             cnt = int(line[1:])
  32.             if op == ord('+'):
  33.                 idx = (idx + cnt) % n
  34.             else:
  35.                 assert op == ord('-'), f"{op} not '-'"
  36.                 idx = (idx - cnt) % n
  37.             process.stdin.write(bytes(f"{v[idx]}\n",
  38.                                       "utf8")), process.stdin.flush()
  39.         opt += 1
  40.         if opt % 100 == 0:
  41.             sys.stderr.write(f"opt get ${opt}\n")
  42.         if opt >= 1000:
  43.             break
  44.  
  45.     raise Exception("Too many try")
  46.  
  47.  
  48. if __name__ == "__main__":
  49.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement