Given an array of 8 prison cells, where 1 represents an occupied cell and 0 represents a vacant cell, simulate the state of the cells after n days. Each day, a cell becomes occupied (1) if both adjacent cells are the same (both 1 or both 0), and becomes vacant (0) otherwise. The first and last cells (cells[0] and cells[7]) will always become 0 as they don't have two adjacent cells.
Return the state of the cells after n days.
Function Signature
def prison_after_n_days(cells: list[int], n: int) -> list[int]:
Rules
-
All 8 cells change at the same moment. Each day's state is computed only from the previous day's state, never from cells already updated that day.
-
The input is the state on day 0, and its end cells may be
0
or
1
. From day 1 on,
cells[0]
and
cells[7]
are always
0
.
Constraints
-
cells.length == 8
-
cells[i]
is either
0
or
1
-
1 <= n <= 10^9
Examples
Input: cells = [0, 1, 0, 1, 1, 0, 0, 1], n = 7
Output: [0, 0, 1, 1, 0, 0, 0, 0]
Applying the rule once gives [0, 1, 1, 0, 0, 0, 0, 0] on day 1, and continuing day by day reaches [0, 0, 1, 0, 1, 1, 0, 0] on day 6. One more day turns that into the output.
Input: cells = [1, 0, 0, 1, 0, 0, 1, 0], n = 1000000000
Output: [0, 0, 1, 1, 1, 1, 1, 0]
Applying the rule 1,000,000,000 times from the input ends on this state. It is also the state the rule produces on day 6.
Input: cells = [1, 1, 1, 1, 1, 1, 1, 1], n = 1
Output: [0, 1, 1, 1, 1, 1, 1, 0]
Every interior cell has two occupied neighbors, so it stays occupied. The two end cells have only one neighbor each, so they become vacant.