# Name: Yikuan Sun
# Team: We Sense
# Intermediate Division
# ACSL Contest #4
# Date of Program: 04/24/20
# Grade: 7
# Language: Python
# Input Data Method: Read from file

def myFunction(inputs):
    import math
    def twodindex(matrix, sample):
        for r in range(len(matrix)):
            for c in range(len(matrix[r])):
                if matrix[r][c] == sample:
                    return (r, c)
                
    def isprime(number):
        factors = 0
        for i in list(range(1, number + 1)):
            if number / i == int(number / i):
                factors = factors + 1
        if factors == 2:
            return True
        else:
            return False
    def placeboard():
        if ghostpos == 52:
            paths[playerpos[0]] = ""
            playerpos.remove(playerpos[0])
        else:
            paths[playerpos[0]] = ""
            paths[ghostpos] = "player"
            playerpos[0] = ghostpos
    outputs = ["error", "error", "error", "error", "error"]
    for x in range(5):
        #try:
            input_splitted = inputs[x].split(" ")
            enemypos = [int(input_splitted[0]), int(input_splitted[1]), int(input_splitted[2])]
            playerpos = [int(input_splitted[3]), int(input_splitted[4]), int(input_splitted[5])]
            r = int(input_splitted[6])
            moves = []
            board = [[0,0,0,0,1,52,0,0,0,0],[0,0,0,0,2,51,0,0,0,0],[7,6,5,4,3,50,49,48,47,46],[8,9,10,11,12,41,42,43,44,45],[17,16,15,14,13,40,39,38,37,36],[18,19,20,21,22,31,32,33,34,35],[0,0,0,0,23,30,0,0,0,0],[0,0,0,0,24,29,0,0,0,0],[0,0,0,0,25,28,0,0,0,0],[0,0,0,0,26,27,0,0,0,0]]
            paths = ["0 is weird", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
            paths[playerpos[0]] = "player"
            paths[playerpos[1]] = "player"
            paths[playerpos[2]] = "player"
            paths[enemypos[0]] = "enemy"
            paths[enemypos[1]] = "enemy"
            paths[enemypos[2]] = "enemy"
            for i in range(r):
                moves.append(int(input_splitted[7 + i]))
            for eachmove in moves:
                playerpos.sort()
                oldposition = playerpos[0]
                ghostpos = playerpos[0]
                for one in range(1):
                    circuit_breaker = False
                    for j in range(eachmove):
                        if ghostpos < 52:
                            ghostpos = ghostpos + 1
                        else:
                            circuit_breaker = True
                    if circuit_breaker:
                        break
                    if paths[ghostpos] != "":
                        ghostpos = playerpos[0]
                        break
                    else:
                        placeboard()
                        if ghostpos == 52:
                            break
                        if isprime(ghostpos):
                            for k in range(6):
                                if paths[ghostpos + 1] == "":
                                    ghostpos = ghostpos + 1
                                else:
                                    placeboard()
                                    break
                            placeboard()
                        elif math.sqrt(ghostpos) == int(math.sqrt(ghostpos)) and ghostpos > 4:
                            for k in range(6):
                                if paths[ghostpos - 1] == "":
                                    ghostpos = ghostpos - 1
                                else:
                                    placeboard()
                                    break
                            placeboard()
                        else:
                            motion = []
                            condition = False
                            for k in range(oldposition, ghostpos):
                                if twodindex(board, k)[0] == (twodindex(board, (k + 1))[0] + 1) or twodindex(board, k)[0] == (twodindex(board, (k + 1))[0] - 1):
                                    motion.append("vertical")
                                    if k - oldposition > 0:
                                        if motion[k - oldposition - 1] == "horizontal":
                                            condition = True
                                else:
                                    motion.append("horizontal")
                            if condition:
                                ghost2 = oldposition
                                goodsquares = []
                                for spaces in list(range(0, eachmove)):
                                    ghost2 = ghost2 + 1
                                    if ghost2 / eachmove == int(ghost2 / eachmove):
                                        if paths[ghost2] == "" or ghost2 == playerpos[0]:
                                            goodsquares.append(ghost2)
                                if len(goodsquares) > 0:
                                    ghostpos = goodsquares[len(goodsquares) - 1]
                                    placeboard()
                                else:
                                    ghostpos = oldposition
                                    placeboard()
            playerpos.sort()
            if len(playerpos) > 0:
                playerpos.sort()
                for item in range(len(playerpos)):
                    playerpos[item] = str(playerpos[item])
                outputs[x] = " ".join(playerpos)
            else:
                outputs[x] = "GAME OVER"
        #except:
        #    seventeen = 17
    return outputs

def input_reader(filename):
    try:
        user_input = open(filename).read()
        print("--Success in reading input data--\n   Obtained the following inputs:\n\n" + str(user_input) + "\n")
    except:
        print("Failure in reading file, please key in inputs:")
        user_input = str(input("1. ")) + "\n" + str(input("2. ")) + "\n" + str(input("3. ")) + "\n" + str(input("4. ")) + "\n" + str(input("5. "))
        print()
    return user_input

output = myFunction(input_reader(input("FILENAME?\n > ")).split("\n"))
print("\n\tOutputs:")
for i in range(5):
    print(str(i + 1) + ". " + output[i])