Ad

You receive a 2d array of size n*m representing a grid in Conway's game of life usinf boolean values. Your goal is to return an new array with boolean values representing the next stage of Conway's Game of Life on a spherical surface (so, top of the grid wraps to bottom and left wraps to right).

The rules for this game are as follows:

  1. Live cell becomes dead if it has less than 2 or more than 3 neighbours
  2. Dead cell becomes live if it has exactly 3 neighbours
def life_next_state(state0: List[List[bool]]) -> List[List[bool]]:
    def count_neighbours(state, row, cell, w, h):
        for i in range(row - 1, row + 2):
            for j in range(cell - 1, cell + 2):
                if (i != row or j != columnIndex) and 0 <= r < w and 0 <= c < h:
                    live_neighbors += int(state[i][j]) 

        return live_neighbors

        
    w, h = len(state0[0]), len(state0)
    state1 = [[None * w] * h]
    for i in range(w):
        for j in range(h):
            is_live = state0[i][j]
            neighbours = count_neighbours(state0, i, j, w, h)
            if is_live and 3 < neighbours < 2:
                state1[i][j] = False
            elif not is_live and neighbours == 3:
                state1[i][j] = True
    return state1