Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in 3rd party category for Ulam–Warburton Automaton by eegoldstraw
import numpy as np
import copy
def add_rows(grid):
"""check if extra rows are need at top and bottom """
if 1 in grid[0]: #if ones in first row add row at top
grid.insert(0,[0]*len(grid[0]))
if 1 in grid[-1]:
# if ones in last row add a row at end
grid.append([0]*len(grid[-1]))
return grid
def transpose_grid(grid):
#convert grid to numpy array and transpose then convert back to list
grid=np.array(grid)
grid_t=grid.T
return grid_t.tolist()
def check_grid(grid):
"""check if extra rows and columns are needed"""
grid=add_rows(grid)
grid_t=transpose_grid(grid)
#transpose list to check columns
grid_t_check=add_rows(grid_t)
#convert back
grid=transpose_grid(grid_t_check)
return grid
def isvalid(y,x,grid):
"""check that x and y are on grid and grid value is 1 """
return y>=0 and y=0 and x int:
"""count cells in pattern after step """
grid=[[1]] #initial grid after step one
cell_count=1 #store count of cells
if step==1: #if step is one return one
return cell_count
for i in range(1,step):
# for each round in step
grid=check_grid(grid) #check if we need to expand grid
#update grid for this step
grid=increment_grid(grid)
cell_count=count_cells(grid)
#count cells for final grid
return cell_count
April 23, 2023