All you have to do here is solve 100 math problems. The program below parses the input and performs the correct operation.
import socket
import re
import time
HOST = 'localhost'
PORT = 9050
def solve():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.connect((HOST, PORT))
except ConnectionRefusedError:
print("Could not connect. Is the server running?")
return
# Read initial welcome message
data = s.recv(4096).decode()
print(data)
# Start the game
s.sendall(b"\\n")
while True:
data = s.recv(4096).decode()
if not data:
break
print(f"Received: {data.strip()}")
if "Flag-" in data:
print("\\n" + "="*40)
print(f"FLAG FOUND: {data.strip()}")
print("="*40)
break
# Look for pattern: Question X/100: A op B = ?
# Sometimes we might receive partial data or multiple lines.
# We specifically look for the line containing the question.
lines = data.split('\\n')
for line in lines:
match = re.search(r'Question \\d+/100: (\\d+) ([+-]) (\\d+) = \\?', line)
if match:
a = int(match.group(1))
op = match.group(2)
b = int(match.group(3))
if op == '+':
ans = a + b
else:
ans = a - b
print(f"Solving: {a} {op} {b} = {ans}")
s.sendall(f"{ans}\\n".encode())
break # Assuming one question per receive for simplicity, though buffering might be needed in a real adverse scenario. Here server waits for answer before sending next.
if __name__ == '__main__':
solve()