Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
O(1) do the maths solution in Speedy category for Hexagon Spiral by altarfinch
# migrated from python 2.7
def to_xy(i):
if i == 1:return 0, 0
r = int(((1+8*((i-2)*1./6))**.5-1)*.5+1)
min_i, max_i = 2+6*r*(r-1)/2, 2+6*r*(r+1)/2
j = i-min_i+1
sixth = (j-1)*6/(max_i-min_i)
return [(j-r,j),(j-r,r),(r,3*r-j),(4*r-j,3*r-j),(4*r-j,-r),(-r,j-6*r)][sixth]
def hex_spiral(first, second):
(f0, f1), (s0, s1) = to_xy(first), to_xy(second)
d0, d1 = s0 - f0 , s1 - f1
return (max,sum)[d0*d1<0](list(map(abs,(d0,d1))))
April 9, 2015
Comments: