Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
906 B
1
Indexable
Never
from typing import List

def update_field(matrix: List[List[str]]) -> List[List[str]]:
    # Find the coordinates of the connected figure
    connected_figure_coords = []
    for i in range(len(matrix)):
        for j in range(len(matrix[0])):
            if matrix[i][j] == 'F':
                connected_figure_coords.append((i, j))
    
    # Update the state of the field after the figure stops falling
    while True:
        is_falling = False
        for i, j in connected_figure_coords:
            if i == len(matrix) - 1 or matrix[i+1][j] == '#':
                continue
            if matrix[i+1][j] == '.':
                matrix[i][j] = '.'
                matrix[i+1][j] = 'F'
                connected_figure_coords.remove((i, j))
                connected_figure_coords.append((i+1, j))
                is_falling = True
        if not is_falling:
            break
    
    return matrix