Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
wrap in a border of zeroes solution in Creative category for Moore Neighbourhood by Leonix
def count_neighbours(grid, row, col):
# Make sure coordinates are not on a border
# by wrapping grid into a frame of zeroes
grid = [(0,)+line+(0,) for line in grid] # left and right
grid += [[0]*len(grid[0])] # bottom
grid = [grid[-1]] + grid # top
# Sum nine cells, then subtract the middle one
return sum(sum(cell for cell in line[col:col+3]) for line in grid[row:row+3]) - grid[row+1][col+1]
April 4, 2019
Comments: