Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Square Spiral by David_Jones
def coords(n):
if n == 1:
return (0,0)
r = int(((n-1)**0.5 + 1) / 2)
side, n = divmod(n - (2*r-1)**2 - 1, 2*r)
n += 1-r
if side == 0:
return (r,n)
if side == 1:
return (-n,r)
if side == 2:
return (-r,-n)
return (n,-r)
def find_distance(first, second):
(x1,y1) = coords(first)
(x2,y2) = coords(second)
return abs(x1-x2) + abs(y1-y2)
June 28, 2019