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LLLLLLLLLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLL
LLLLL.LL.LLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLL.LLLLL.LLLLLL..LLLLLLLLLLLLLLLL..LLLLLL
LLLLL.L.LL.LLLL.LLLL.LLLLLL..LLLLLL.LLLLLLLLLLL.LLLLLLLLLLL.LLLLLLL.LLLLLL.LLLLLL.LLLLLLLLLLLLLLLL
LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLL..LLLLLLLL.LLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL
LLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLL..LLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL
LLLLL.LLLL.LLLLLLLLLLLLL.LLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL
LLLLL.LLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.L.LLLLLLL.LL.LLLLLLL.LL.LLLLLL.LLLLLL.LLLLLLLLL..LLLLL
LLL.L.LLLL.LLLLLLLLL.LLLLLL..LLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLL.LLLL.LLLLLLL.LLLLL.LLLLLLLLL.LLLLLL
L.LLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLL.LLLL..LLLLLLLLLLLLLLLLLL.LLL.LLLLLL
"""

import itertools, time

def get_seat_type(seats, coords) :
    return seats[coords[1]][coords[0]]

def get_surroundings(seats, coords) :
    surroundings = []
    for combo in itertools.product([-1, 0, 1], [-1, 0, 1]) :
        try :
            if combo != (0, 0) :
                x_factor, y_factor = 0, 0
                while True :
                    new_addition = list((combo[0]+coords[0]+x_factor, combo[1]+coords[1]+y_factor))
                    try :
                        new_seat = seats[new_addition[1]][new_addition[0]]
                        if new_seat != '.' :
                            if new_addition[0] >= 0 and new_addition[1] >= 0 :
                                surroundings.append(new_seat)
                            break
                        else :
                            x_factor += combo[0]
                            y_factor += combo[1]
                    except :
                        break
                        
        except :
            pass
    return surroundings

def determine_new_seat(seat_type, surroundings) :
    if seat_type == 'L' and '#' not in surroundings :
        return '#'
    elif seat_type == '#' and surroundings.count('#') >= 5 :
        return 'L'
    else :
        return seat_type

def update_seats(seats) :
    x_max, y_max = len(seats[0]), len(seats)
    updated_seats = []
    for y in range(y_max) :
        new_row = ''
        for x in range(x_max) :
            coords = [x, y]
            seat_type = get_seat_type(seats, coords)
            if seat_type == '.' :
                new_row = new_row + '.'
            else :
                surroundings = get_surroundings(seats, coords)
                new_addition = determine_new_seat(seat_type, surroundings)
                new_row = new_row + new_addition
        updated_seats.append(new_row)
    return updated_seats

def main(seats) :
    while True :
        updated_seats = update_seats(seats)
        """for row in updated_seats :
            print(row)
        print()"""
        if seats == updated_seats :
            return ''.join(updated_seats).count('#')
        seats = updated_seats

seats = seats[1:][:-1].split('\n')
start = time.time()
print(main(seats))
end = time.time()
print('Runtime: ', end-start)
