Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
2 line different from Ulam original solution in Clear category for Ulam-Warburton Automaton Hex by dig
from math import sqrt
#since there are floats in coordenates, we should work with some precition
def almost_equal(tup1, tup2):
return abs(tup1[0]-tup2[0])<0.00001 and abs(tup1[1]-tup2[1])<0.00001
def free_neighbours(node):
# Define the neighboring cells as tuples
neighbours = ((0,2),(sqrt(3),1),(sqrt(3),-1),(0,-2),(-sqrt(3),-1),(-sqrt(3),1))
for x, y in neighbours:
counter = 0
# Count the number of empty neighboring cells
for i, j in neighbours:
for nod in nodes:
if almost_equal(nod, (node[0]+x+i, node[1]+y+j)):
counter+=1
# If there are only one neigboue (the father) then is free
if counter == 1:
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) == 7
assert automaton(3) == 13
assert automaton(4) == 31
assert automaton(5) == 37
print("The mission is done! Click 'Check Solution' to earn rewards!")
March 18, 2023
Comments: