Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Toothpick of Destiny solution in Clear category for Toothpicks by BootzenKatzen
def toothpicks(step: int) -> int:
if step == 0:
return 0
if step == 1:
return 1
toothpicks = 1
available_ends = [(0, 1), (0, -1)]
all_ends = []
for i in range(2, step+1):
new_ends = []
for end in available_ends:
if i % 2 == 0:
new_ends.append((end[0] + 1, end[1]))
new_ends.append((end[0] - 1, end[1]))
elif i % 2 == 1:
new_ends.append((end[0], end[1] + 1))
new_ends.append((end[0], end[1] - 1))
toothpicks += 1
all_ends += available_ends
available_ends = [i for i in new_ends if new_ends.count(i) == 1 and i not in all_ends]
return toothpicks
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!")
Dec. 13, 2023