Files
Tuan-Dat Tran 3a3a5587ca feat: add timer
Signed-off-by: Tuan-Dat Tran <tuan-dat.tran@dextradata.com>
2026-03-21 13:23:39 +01:00

45 lines
1015 B
Python

import math
import re
import socket
import time
HOST = "challenge01.root-me.org"
PORT = 52002
def solve_prompt(prompt: str) -> str:
match = re.search(
r"square root of\s*(\d+)\s*and multiply by\s*(\d+)",
prompt,
re.IGNORECASE,
)
if not match:
raise ValueError("Unsupported challenge prompt")
left = int(match.group(1))
right = int(match.group(2))
return f"{math.sqrt(left) * right:.2f}"
def main() -> None:
conn = socket.create_connection((HOST, PORT), timeout=2)
start_time = time.monotonic()
try:
prompt = conn.recv(4096).decode(errors="replace")
print(prompt, end="")
answer = solve_prompt(prompt)
conn.sendall((answer + "\n").encode())
result = conn.recv(4096).decode(errors="replace")
print(result, end="")
finally:
conn.close()
elapsed = time.monotonic() - start_time
print(f"\nConnection lifetime: {elapsed:.3f}s")
if __name__ == "__main__":
main()