A wave through the audience

Write code · Challenge · §9.3 Nearest first

The seats of a live show recorded with a studio audience are given as the grid seats. 'O' is a seat where an audience member is sitting, '.' is an empty seat, and 'W' is the seat of an audience member who is already standing at the start to begin the wave.

Every second, all sitting audience members in the seats directly above, below, left of, or right of a standing audience member stand up together. Once an audience member stands up, they stay standing. An empty seat cannot pass the wave on. Complete the function solution, which returns how many seconds it takes until every audience member is standing. If nobody is sitting at the start, return 0. If some audience member never stands up, return -1.

Function

solution(seats)

Constraints

Examples

seatsresult
['OOO', 'O.O', 'OOW']4
['OOO.OO', 'OOW.OW']3

Starter code

def solution(seats):
    answer = 0
    return answer

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.