#!/usr/bin/env python3

import http.client
import random
import urllib.parse


def randint_compat(lo, hi):
    return lo + int(random.random() * (hi + 1 - lo))


def generate_combination():
    return ["-".join(f"{randint_compat(0, 99):02}" for _ in range(10)) for _ in range(3)]


conn = http.client.HTTPConnection("challenges.ringzer0team.com:10298")

conn.request("GET", "/")
resp = conn.getresponse()
cookie = resp.getheader("Set-Cookie").split(";")[0]
resp.read()

params = urllib.parse.urlencode({
    "combination": "12-34-56-78-91-23-45-67-89-12"
})
headers = {
    "Content-type": "application/x-www-form-urlencoded",
    "Cookie": cookie
}
conn.request("POST", "/", params, headers)
resp = conn.getresponse()
html = resp.read().decode()
last_win = html.split("The winning combination was : ")[1][0:29]

for i in range(32768):
    random.seed(i)
    combination = generate_combination()
    if combination[0] == last_win:
        break

params = urllib.parse.urlencode({
    "combination": combination[1]
})
conn.request("POST", "/", params, headers)
resp = conn.getresponse()
html = resp.read().decode()

conn.close()
print(html)
