import time
import arrow
import subprocess
from pwn import *

n = 142592923782837889588057810280074407737423643916040668869726059762141765501708356840348112967723017380491537652089235085114921790608646587431612689308433796755742900776477504777927984318043841155548537514797656674327871309567995961808817111092091178333559727506289043092271411929507972666960139142195351097141
#r = remote("cryptoclock.tuctf.com", 1230)
r = remote("127.0.0.1", 1234)

def read_menu(r):
    r.readline()
    r.readline()
    r.readline()
    r.readline()
    r.readline()

def read_ntp_fake(r):
    r.readline()
    r.readline()


def remove_duplicates(res):
    final = []
    last_one = -1
    for i in sorted(res, key=lambda x:x[0].timestamp):
        if i[0].timestamp == last_one:
            continue

        last_one = i[0].timestamp
        final.append(i)

    final_filtered = sorted(final, key=lambda x:x[0].timestamp)
    return final_filtered[:5]


timestamp_connection_utc = int(arrow.utcnow().timestamp)
res = []
while int(arrow.utcnow().timestamp) < timestamp_connection_utc + 7:
    read_menu(r)
    r.sendline("1")
    read_ntp_fake(r)
    fake_time = int(r.readline().decode().strip())
    res.append((arrow.utcnow(), fake_time))
    time.sleep(0.4)

enc_time0, enc_time1, enc_time2, enc_time3, enc_time4 = remove_duplicates(res)

X = (enc_time1[1] + enc_time4[1] - enc_time2[1] - enc_time3[1] - 30) % n
while X % 12 != 0:
    X += n

time0 = int((X//12) % n)
offset = time0 - timestamp_connection_utc
assert enc_time0[1] == pow((timestamp_connection_utc + offset), 3, n)

time = arrow.utcnow().timestamp + offset
guessing_int = subprocess.check_output(["python2", "-c", "import random;random.seed({});print random.randint(0,999999999999)".format(time)]).decode().strip()

read_menu(r)
r.sendline("2")
r.readline()
r.sendline(guessing_int)
print(r.readline())
print(r.readline())
print(r.readline())
print(r.readline())