Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using [x+dx][y+dy] in range solution in Clear category for Moore Neighbourhood by johnhaines
def count_neighbours(grid, x, y):
""" Rotate around a grid cell given by x and y coordinate and count instances of 1
This includes the cell itself - so we subtract it at the end.
"""
return sum(grid[x+dx][y+dy] == 1 for dx in range(-1,2) for dy in range(-1,2) if 0<=x+dx
July 4, 2015
Comments: