Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
HexagonSpiral (Constant Time) solution in Speedy category for Hexagon Spiral by bunnychai
def cube(k):
if k == 1:
return 0, 0, 0
y = int((3 + (12 * k - 15) ** 0.5) / 6)
x, z = -y, 0
r, step = divmod(3 * y * (y + 1) + 1 - k, y)
c = [x, y, z]
s = -1 if r & 1 else 1
x, y, z = [s * c[r % 3], s * c[(r + 1) % 3], s * c[(r + 2) % 3]]
d = ((0, -1, 1), (1, -1, 0), (1, 0, -1),
(0, 1, -1), (-1, 1, 0), (-1, 0, 1))[r]
return x + d[0] * step, y + d[1] * step, z + d[2] * step
def hex_spiral(first, second):
return max(abs(a - b) for a, b in zip(cube(first), cube(second)))
Nov. 12, 2014
Comments: