Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
cell = 2D point solution in Clear category for Ulam–Warburton Automaton by dig
def free_neighbours(node):
# Define the neighboring cells as tuples
neighbours = ((1,0),(-1,0),(0,1),(0,-1))
for x, y in neighbours:
counter = 0
# Count the number of empty neighboring cells
for i, j in neighbours:
if (x+i, y+j) != (0,0) and (node[0]+x+i, node[1]+y+j) not in nodes:
counter += 1
# If there are exactly 3 empty neighboring cells, yield the tuple of this cell
if counter == 3:
yield ((x,y))
def automaton(step: int) -> int:
# Define the nodes set as a global variable and initialize it to contain only the cell (0,0)
global nodes
nodes = last_generation_nodes = {(0,0)}
# Generate the next generation of cells
for _ in range(step-1):
news = set()
# For each cell in the last generation, generate a set of the free neighboring cells
for node in last_generation_nodes:
for x, y in free_neighbours(node):
# Add the free neighboring cells to the set of new cells
news.add((node[0]+x, node[1]+y))
# Update the last generation to the new generation
last_generation_nodes = news
# Update the set of all nodes with the new generation
nodes.update(news)
return len(nodes)
print("Example:")
print(automaton(2))
# These "asserts" are used for self-checking
assert automaton(1) == 1
assert automaton(2) == 5
assert automaton(3) == 9
assert automaton(4) == 21
assert automaton(5) == 25
print("The mission is done! Click 'Check Solution' to earn rewards!")
March 15, 2023