Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Coordenates mathod solution in Clear category for Toothpicks by dig
def free_node(node):
if {node, (node[0], node[1]+2)} in edges and {node, (node[0], node[1]-2)} in edges:
return False
elif {node, (node[0]+2, node[1])} in edges and {node, (node[0]-2, node[1])} in edges:
return False
else:
return True
def toothpicks(step: int) -> int:
global edges
edges = { frozenset([(0,-1), (0,1)]) }
global nodes
nodes, last_generation_nodes = {(0,-1),(0,1)}, {(0,-1),(0,1)}
for _ in range(step-1):
new_nodes = set()
for node in last_generation_nodes:
if free_node(node):
if {node, (node[0], node[1]+2)} in edges or {node, (node[0], node[1]-2)} in edges:
new_nodes.update({(node[0]+1,node[1]), (node[0]-1,node[1])})
edges.add(frozenset([(node[0]+1,node[1]), (node[0]-1,node[1])]))
elif {node, (node[0]+2, node[1])} in edges or {node, (node[0]-2, node[1])} in edges:
new_nodes.update({(node[0],node[1]+1), (node[0],node[1]-1)})
edges.add(frozenset([(node[0],node[1]+1), (node[0],node[1]-1)]))
nodes.update(new_nodes)
last_generation_nodes = new_nodes
return len(edges)
print("Example:")
print(toothpicks(2))
# These "asserts" are used for self-checking
assert toothpicks(1) == 1
assert toothpicks(2) == 3
assert toothpicks(3) == 7
assert toothpicks(4) == 11
assert toothpicks(5) == 15
print("The mission is done! Click 'Check Solution' to earn rewards!")
March 14, 2023