Ruby (110 bytes)
n=a.size
b=[*(0...n)]
b.product(b).group_by{|i,j|i+j}.flat_map{|_,f|f.sort.map{|i,j|a[i][j]}}.each_slice(n).to_a
#=> [[1, 2, 1, 3, 2],
# [1, 4, 3, 2, 1],
# [5, 4, 3, 2, 1],
# [5, 4, 3, 2, 5],
# [4, 3, 5, 4, 5]]
The sort operation may not be required, but the doc for Enumerable#group_by does not guarantee the ordering of values in the hash values (which are arrays), but current versions of Ruby provide the ordering one would expect and the ordering I would need if sort were removed from my code.
The steps are as follows.
n=a.size
#=> 5
b=[*(0...n)]
#=> [0, 1, 2, 3, 4]
c = b.product(b)
#=> [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 0], [1, 1], [1, 2], [1, 3],
# [1, 4], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [3, 0], [3, 1], [3, 2],
# [3, 3], [3, 4], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4]]
d=c.group_by{|i,j|i+j}
#=> {0=>[[0, 0]],
# 1=>[[0, 1], [1, 0]],
# 2=>[[0, 2], [1, 1], [2, 0]],
# 3=>[[0, 3], [1, 2], [2, 1], [3, 0]],
# 4=>[[0, 4], [1, 3], [2, 2], [3, 1], [4, 0]],
# 5=>[[1, 4], [2, 3], [3, 2], [4, 1]],
# 6=>[[2, 4], [3, 3], [4, 2]],
# 7=>[[3, 4], [4, 3]],
# 8=>[[4, 4]]}
e=d.flat_map{|_,f|f.sort.map{|i,j|a[i][j]}}
#=> [1, 2, 1, 3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 5, 4, 3, 5, 4, 5]
f=e.each_slice(n)
#=> #<Enumerator: [1, 2, 1, 3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2,
# 5, 4, 3, 5, 4, 5]:each_slice(5)>
Lastly, f.to_a returns the array shown earlier.
Next time, please CAPITALIZE things. – Oliver Ni – 2016-11-23T20:18:08.980
How does this work if the original array has a length other than 5? – None – 2016-11-23T20:18:10.907
@ais523 I'm assumming its the same thing, you just replace 'five' with the length – Oliver Ni – 2016-11-23T20:22:27.350
Can we assume the numbers always be positive integers? – Luis Mendo – 2016-11-23T20:30:00.163
luis mendo- yes – None – 2016-11-23T20:32:26.450
oliver- yes that is correct – None – 2016-11-23T20:32:32.033
7@JohnCena You shouldn't accept the first answer, you need to give the post some time to gain traction and some more answers. – Kade – 2016-11-23T20:35:33.850
How do you get that triangle by reading along the diagonals? – Peter Taylor – 2016-11-23T21:47:24.750
@PeterTaylor They are anti-diagonals, in a matrix sense – Luis Mendo – 2016-11-23T22:04:37.473