-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_layer_rotation.rb
47 lines (38 loc) · 1.19 KB
/
matrix_layer_rotation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def matrixRotation(matrix, r)
rows = matrix.size
cols = matrix[0].size
mid = [rows, cols].min / 2
rings = []
(0...mid).each do |index|
aux = []
(index...rows - 1 - index).each {|i| aux << matrix[i][index] }
(index...cols - 1 - index).each {|i| aux << matrix[rows - 1 - index][i] }
(rows - 1 - index).step(index + 1, -1).each {|i| aux << matrix[i][cols - 1 - index] }
(cols - 1 - index).step(index + 1, -1).each {|i| aux << matrix[index][i] }
rings << aux
end
result = (1..( rows * cols )).each_slice(cols).to_a
(0...mid).each do |index|
ring = rings[index]
shift = r % ring.size
ring.rotate!(-shift)
idx = 0
(index...rows - 1 - index).each do |i|
result[i][index] = ring[idx]
idx += 1
end
(index...cols - 1 - index).each do |i|
result[rows - 1 - index][i] = ring[idx]
idx += 1
end
(rows - 1 - index).step(index + 1, -1).each do |i|
result[i][cols - 1 - index] = ring[idx]
idx += 1
end
(cols - 1 - index).step(index + 1, -1).each do |i|
result[index][i] = ring[idx]
idx += 1
end
end
result.each { |row| puts "#{row.join(" ")}" }
end