Infection Spread Simulation

Asked at OpenAIPhone screen

Reported by the problem's creator, not verified

Simulate an infection spreading on a rectangular grid, day by day. The grid passed in contains only 0 (healthy) and 1 (infected); its 1 cells are infected on day 0.

Neighbors of a cell are the up to 8 cells touching it horizontally, vertically or diagonally. All cells change at the same moment: every decision on a day uses the grid as it stood at the end of the previous day.

simulate(grid, infect_threshold, days) returns the grid after days days.

Examples

  1. Example 1

    Input: grid = [[0,0,0],[0,1,0],[0,0,0]], infect_threshold = 1, days = 2

    Output: [[1,1,1],[1,1,1],[1,1,1]]

    Explanation: With threshold 1, every healthy cell touching the infected center, including diagonally, becomes infected on day 1. Day 2 changes nothing because every cell is already infected.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.