joylintp

solve.py

Jan 23rd, 2021
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import time
  5. import socket
  6.  
  7. host = "chall.ctf.bamboofox.tw"
  8. port = 10069
  9.  
  10. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11. s.connect((host, port))
  12. print('connect success')
  13.  
  14. def main():
  15.     print('main start')
  16.     _ = s.recv(4096)
  17.     # print(_.decode('utf-8'))
  18.  
  19.     s.send(bytes('EXCRUCIATING\n', 'utf-8'))
  20.  
  21.     _ = s.recv(4096)
  22.     # print(_.decode('utf-8'))
  23.     s.send(bytes('\n', 'utf-8'))
  24.    
  25.     cnt = 0
  26.     while cnt < 32:
  27.         cnt += 1
  28.         print("Recv!")
  29.         raw = s.recv(8192)
  30.         print(f"raw = {raw}")
  31.         raw = raw.decode('utf-8')
  32.         if raw.find("Wrong answer") != -1 or raw.find("Time limit exceeded") != -1:
  33.             exit(raw + " QAQ\n")
  34.  
  35.         l = raw.find("====== BEGIN INPUT =======") + 27
  36.         r = raw.find("====== END OF INPUT ======") - 1
  37.         print(f'Testcase #{cnt}\n{raw[l:r]}\n')
  38.         with open('in.txt', 'w') as f:
  39.             f.write(raw[l:r])
  40.         os.system("./solve < in.txt | ./sparseElim | python crt.py > out.txt")
  41.         with open('out.txt', 'r') as f:
  42.             ans = f.read(1024)
  43.             if ans.find('.') != -1:
  44.                 ans = ans[:ans.find('.')]
  45.             ans = str(int(ans))
  46.             print("ans =", ans)
  47.             s.send(bytes(ans + '\n', 'utf-8'))
  48.  
  49.         print("OK!")
  50.         time.sleep(0.01)
  51.     ans = s.recv(1024)
  52.     print(ans)
  53.  
  54. main()
Advertisement
Add Comment
Please, Sign In to add comment