#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p "python3.withPackages (ppkgs: with ppkgs; [ pwntools ])"

from pwn import *

ADDR, PORT, *_ = "fickle-tempest.picoctf.net 61388".split()

def recv_question(r: remote) -> list[str]:
    r.recvuntil(b'the')
    xs = r.recvuntil(b'as a word.').decode().removesuffix('as a word.').strip().split(' ')
    return xs

def send_answer(r: remote, answer: str):
    r.recvuntil(b'Input:\n')
    r.sendline(answer.encode())

def main():
    r = remote(ADDR, PORT)

    binary = recv_question(r)
    answer = ''.join(chr(int(c, 2)) for c in binary)
    print(f"{binary} -> {answer}")
    send_answer(r, answer)

    octals = recv_question(r)
    answer = ''.join(chr(int(c[1:], 8)) for c in octals)
    print(f"{octals} -> {answer}")
    send_answer(r, answer)

    hx = recv_question(r)[0]
    hx_chunks = [hx[i:i+2] for i in range(0,len(hx),2)]
    answer = ''.join(chr(int(c, 16)) for c in hx_chunks)
    print(f"{hx} -> {answer}")
    send_answer(r, answer)

    print(r.recvall().decode())
    r.close()

if __name__ == "__main__":
    main()
