0

I'm having trouble trying to simulate a cellular automaton for a Google Foobar challenge. The rules for this automaton are as follows:

You find that the current existence of gas in a cell of the grid is determined exactly by its 4 nearby cells, 
specifically, (1) that cell, (2) the cell below it, (3) the cell to the right of it, and (4) the cell below and to the right of it. 
If, in the current state, exactly 1 of those 4 cells in the 2x2 block has gas, then it will also have gas in the next state. 
Otherwise, the cell will be empty in the next state.

My (very naive) Python attempt is below:

def index_exists_2d(array, x, y):
    return x < len(array[0]) and y < len(array)

def sliding_window(array):
    array = [[int(j) for j in i] for i in array]
    new_nebula = [[None] * (len(array[0])-1)] * (len(array)-1)
    debug = 0
    for y in range(len(array)):
        for x in range(len(array[0])):
            if index_exists_2d(array, x+1, y):
                if index_exists_2d(array, x, y+1):
                    if index_exists_2d(array, x+1, y+1):
                        debug += 1
                        print(array[y])
                        flat_window = [array[y][x], array[y][x+1], array[y+1][x], array[y+1][x+1]]
                        if sum(flat_window) == 1:
                            new_nebula[y][x] = True
                        else:
                            new_nebula[y][x] = False

    return new_nebula

Input test to function sliding_window:

[[False, True, False, False],
[False, False, True, False], 
[False, False, False, True], 
[True, False, False, False]]

Expected output of function sliding_window:

[[True, False, True],
[False, True, False],
[True, False, True]]

Actual output of function sliding_window:

[[True, False, True],
[True, False, True],
[True, False, True]]
ZeroMaxinumXZ
  • 357
  • 2
  • 6
  • 21

0 Answers0