Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Square Spiral by flpo
def spiral(n):
from math import ceil, sqrt
k = ceil((sqrt(n)-1)/2)
t = 2*k
m = (t+1)**2
if n >= m-t:
return k-(m-n), -k
else:
m = m-t
if n >= m-t:
return -k,-k+(m-n)
else:
m = m-t
if n >= m-t:
return -k+(m-n),k
return k,k-(m-n-t)
def find_distance(*points):
(i1, j1), (i2, j2) = map(spiral, points)
return abs(i2 - i1) + abs(j2 - j1)
Aug. 28, 2017
Comments: