Without loss of generality, write an array as:
arr = [ [ 1, 2, 3, 4,], [12, 13, 14, 5,], [11, 16, 15, 6,], [10, 9, 8, 7,] ]
Desired Result:
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
I use the helper:
def rotate_anticlockwise(arr) arr.map(&:reverse).transpose end
For instance:
rotate_anticlockwise(arr) #=> [[4, 5, 6, 7], # [3, 14, 15, 8], # [2, 13, 16, 9], # [1, 12, 11, 10]]
Now we can calculate the desired result as follows:
out = [] a = arr.map(&:dup) while a.any? out.concat(a.shift) a = rotate_anticlockwise(a) end out # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]