Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Cube coordinates solution in Clear category for Hexagon Spiral by HeNeArKr
# 1. Build up list of 'cube coordinates' for each cell up to larger of
# first and second.
# 2. Calculate distance between cells using cube coordinates.
#
# I used many ideas from
# Red Blob Games (https://www.redblobgames.com/grids/hexagons/)
# especially the use of cube coordinates.
# 0: upper left side of hex ring; next cell is to upper right
# 1: upper right side of hex ring; next cell is to lower right
# etc.
TO_NEXT_HEX = {0: (1, 0, -1), 1: (1, -1, 0), 2: (0, -1, 1),
3: (-1, 0, 1), 4: (-1, +1, 0), 5: (0, 1, -1)}
def hex_spiral(first, second):
""" Returns the number of cells between 'first' and 'second', when cells
are numbered in a spiral manner.
"""
last_cell = max(first, second)
cells = [(0, 0, 0)] # list of each cell's coordinates; cell num = idx+1
r = 1 # describes ring radius and side length
count = 1
x, y, z = 0, 0, 0
done = False
while not done:
side = 0
# New ring numbering starts from the cell directly above the last cell
# of the previous ring. In order to treat all sides identically, I
# use the cell to the upper left of the previous ring's last cell as
# the 'zero-th' cell of the new ring.
x -= 1
y += 1
while side < 6 and not done:
num = 0
dx, dy, dz = TO_NEXT_HEX[side]
while num < r and not done:
x, y, z = x+dx, y+dy, z+dz
cells.append((x, y, z))
count += 1
num += 1
if count > last_cell:
done = True
side += 1
r += 1
x1, y1, z1 = cells[first-1]
x2, y2, z2 = cells[second-1]
return max(abs(x2-x1), abs(y2-y1), abs(z2-z1))
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert hex_spiral(2, 9) == 1, "First"
assert hex_spiral(9, 2) == 1, "Reverse First"
assert hex_spiral(6, 19) == 2, "Second, short way"
assert hex_spiral(5, 11) == 3, "Third"
assert hex_spiral(13, 15) == 2, "Fourth, One row"
assert hex_spiral(11, 17) == 4, "Fifth, One more test"
April 1, 2019