import sys
import os
from multiprocessing.connection import Listener, Client


listener = Listener(("localhost", 6001), authkey=b"secret password")
conn_input = Client(("localhost", 6000), authkey=b"secret password")
conn_output = listener.accept()

DEBUG = False

if DEBUG:
    DEBUG_FILE = os.path.join(os.path.dirname(__file__), "debug.txt")
    with open(DEBUG_FILE, "w") as f:
        pass

if DEBUG:
    with open(DEBUG_FILE, "a") as f:
        f.write("Init game info\n")
number_of_cells = int(input())
conn_input.send(str(number_of_cells))
for i in range(number_of_cells):
    conn_input.send(
        input()
    )  # cell_index, richness, neigh_0, neigh_1, neigh_2, neigh_3, neigh_4, neigh_5
if DEBUG:
    with open(DEBUG_FILE, "a") as f:
        f.write("done\n\n")

# game loop
done = False
while not done:
    if DEBUG:
        with open(DEBUG_FILE, "a") as f:
            f.write("Turn\n")
    a = input()
    if DEBUG:
        with open(DEBUG_FILE, "a") as f:
            f.write(f"Sending {a}...\n")
    conn_input.send(a)  # day
    if DEBUG:
        with open(DEBUG_FILE, "a") as f:
            f.write(f"Turn started by sending {a}...\n")
    conn_input.send(input())  # nutrients
    conn_input.send(input())  # sun, score
    conn_input.send(input())  # other_sun, other_score, other_is_waiting
    number_of_trees = int(input())
    conn_input.send(str(number_of_trees))
    for i in range(number_of_trees):
        conn_input.send(input())  # cell_index, size, tree_owner, is_dormant
    possible_move_number = int(input())
    conn_input.send(str(possible_move_number))

    for i in range(possible_move_number):
        conn_input.send(input())

    if DEBUG:
        with open(DEBUG_FILE, "a") as f:
            f.write("Waiting action...\n")

    action, done = conn_output.recv()

    if DEBUG:
        with open(DEBUG_FILE, "a") as f:
            f.write("Action received\n")
            f.write("Sending action...\n")

    print(action)

    if DEBUG:
        with open(DEBUG_FILE, "a") as f:
            f.write("Sending confirmation...\n")

    conn_input.send(done)

    if DEBUG:
        with open(DEBUG_FILE, "a") as f:
            f.write("Turn done\n\n")


conn_input.close()
listener.close()
