Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Square Spiral by UFO665
def table():
t = [[0 for i in range(33)] for j in range(33)]
row = col = rowMin = rowMax = colMin = colMax = 16
rowDelta = -1
colDelta = 0
i = 1
while i <= 33 ** 2:
t[row][col] = i
i += 1
row += rowDelta
col += colDelta
if row < rowMin:
rowMin = row
rowDelta = 0
colDelta = 1
elif row > rowMax:
rowMax = row
rowDelta = 0
colDelta = -1
elif col < colMin:
colMin = col
rowDelta = -1
colDelta = 0
elif col > colMax:
colMax = col
rowDelta = 1
colDelta = 0
return t
def find_distance(first, second):
r1 = r2 = c1 = c2 = None
for i, row in enumerate(table()):
if first in row:
r1 = i
c1 = row.index(first)
if second in row:
r2 = i
c2 = row.index(second)
if all(j for j in [r1, r2, c1, c2]):
break
return abs(r1 - r2) + abs(c1 - c2)
Jan. 21, 2016