Problem 289513 · medium · Phase 02 Linear Data Structures

Waiting Room Shuffle

2d-lists · simulation · neighbours

A waiting room is a grid of strings: "L" is an empty seat, "#" an occupied seat and "." floor. People move according to these rules, all applied simultaneously to produce the next layout:

  • an empty seat with no occupied seats among its eight neighbours becomes occupied,
  • an occupied seat with four or more occupied neighbours becomes empty,
  • every other cell (including floor) stays as it is.

Return the new grid after one round.

Examples

Input:  room = [["L", "L"],
                ["L", "L"]]
Output: [["#", "#"],
         ["#", "#"]]

Input:  room = [["#", "#", "#"],
                ["#", "#", "#"]]
Output: [["#", "L", "#"],
         ["#", "L", "#"]]
Explanation: the middle seats each see five occupied neighbours; the corners see only three.

Input:  room = [["L", ".", "L"]]
Output: [["#", ".", "#"]]

Constraints

  • 1 <= rows, cols <= 100
  • The input grid must not be modified.

Goals

  • Apply different rules to different cell types
  • Count occupied 8-directional neighbours while ignoring floor
Starting Python…