l1 = Flat((18, 13, 7))

gates = []

rows = csv.split("\n")
i = 0
for row in rows:
    # board1 gates: 3 - 29 // board2 gates: 3 - 54
    if i > 3 and i < 29:
        columns = row.split(",")
        gates.append(np.array(columns))
    i += 1
for gate in np.array(gates):
    l1.is_gate((int(gate[1]), int(gate[2]), 0), gate[0])

routes = []
j = 0
for row in rows:
    # board 1 : 1{31 - 62} // 2{64 - 105} // 3{107 - 158}
    # board 2 : 1{56 - 107} // 2{109 - 170} // 3{172 - 243}
    if j > 31 and j < 62:
        columns = row.split(",")
        routes.append(np.array(columns))
    j += 1

gate_1 = l1.find_gate(24)
gate_2 = l1.find_gate(5)

l1.find_route(gate_1, gate_2, '01')
print(l1)
for row in rows:
    if j > 31 and j < 62:
        columns = row.split(",")
        routes.append(np.array(columns))
    j += 1

lengths = []
sorted_routes = []
for route in routes:
    gate1 = l1.find_gate(int(route[0]))
    gate2 = l1.find_gate(int(route[1]))
    lengths.append([gate1, gate2, gate1.man_distance(gate2)])
    # i = 0
    # while (i < len(sorted_routes)):
length = 0
# lengths.sort(key=lambda x: x[2])
failed_routes = 0
route_num = 1
for route in lengths:
    if route_num < 31:
        made_route = l1.find_route(route[0], route[1], route_num)
        if made_route is None:
            failed_routes += 1
        else:
            length += len(made_route)
    route_num += 1

print(l1)
print(failed_routes)
print(length)
j = 0
for row in rows:
    if j > 31 and j < 62:
        columns = row.split(",")
        routes.append(np.array(columns))
    j += 1

route_number = 1
failed_routes = 0
length = 0
for route in routes:
    if route_number < 31:
        route_num = ''
        if route_number < 10:
            route_num = '0'
        route_num += str(route_number)
        gate1 = l1.find_gate(int(route[0]))
        gate2 = l1.find_gate(int(route[1]))
        made_route = l1.find_route(gate1, gate2, route_num)
        if made_route is None:
            failed_routes += 1
        else:
            length += len(made_route)
    route_number += 1

print(l1)
print(failed_routes)
print(length)
f = open('output.txt', 'w')
print(l1, file=f)
            gates.append(np.array(columns))
        i += 1
    for gate in np.array(gates):
        l1.is_gate((int(gate[1]), int(gate[2]), 0), gate[0])

    failed_routes = 0
    failed_route_list1 = []
    for route in routes:
        route_num = ''
        if route_number < 10:
            route_num = '0'
        route_num += str(route_number)
        gate1 = l1.find_gate(int(route[0]))
        gate2 = l1.find_gate(int(route[1]))
        if l1.find_route_sub_goal(gate1, gate2, route_num) is None:
            if l1.find_route(gate1, gate2, route_num) is None:
                failed_route_list1.append(route_num)

                failed_routes += 1
        route_number += 1

    # set divisor to number of routes
    average_fail = failed_routes / 50

    if run_time == 99:
        print('retry on lesser route')
        del all_routes[-1]
        if len(all_routes) == 0:
            print('reset to random route')
            random.shuffle(routes)
            failed_route_list = []