# # Код на ruby 2.2.3p173
def LineUpArraySize(num)
cur_size = 4
while num > cur_size
cur_size = 2 * cur_size + 8
end
cur_size = 2 * cur_size + 8
end
def getSpiralNeighbours(num)
a = []
a[0] = [0, 0]
a[1] = [-1, 1]
moves = [[1, 0], [0, -1], [-1, 0], [0, 1]]
move_direction = 0
side_size = 2
cur_size = 1
cycle = 1
mode_direction = 0
for i in 2..LineUpArraySize(num)
new_num = Array.new(2)
new_num[0] = a.last[0] + moves[move_direction][0]
new_num[1] = a.last[1] + moves[move_direction][1]
a[i] = new_num
# p new_num
cur_size += 1
if cur_size == side_size
cur_size = 1
move_direction = (move_direction + 1) % 4
side_size += 1 if move_direction % 2 == 0
cycle += 1 if move_direction == 0
end
end
a.each_with_index do |i,x|
p [x, i] if (i[0]-a[num][0]).abs <= 1 and (i[1]-a[num][1]).abs <= 1<br> end
end
getSpiralNeighbours(1090)
Вывод (числа и условные координаты)
[962, [15, 16]]
[1089, [15, 17]]
[1090, [16, 17]]
[1091, [16, 16]]
[1224, [15, 18]]
[1225, [16, 18]]
[1226, [17, 18]]
[1227, [17, 17]]
[1228, [17, 16]]