-
Notifications
You must be signed in to change notification settings - Fork 21
/
traverse_spiral_matrix.rb
43 lines (36 loc) · 1.04 KB
/
traverse_spiral_matrix.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
# Traverse 2D Array in a spiral
# Set 4 variables to define the outer bounds
# of the matrix - left, right, top, bottom
# Set a direction: 0 -> left to right, 1 -> top to bottom,
# 2 -> right to left, 3 -> bottom to top
# At each iteration check which direction is set and
# iterate through those values in the matrix
# Then increment the outer bound by 1 to show
# that that area has already been traced
def traverse_spiral(matrix)
l = 0
r = matrix[0].length - 1
b = matrix.length - 1
t = 0
dir = 0
# loop until top bound equals bottom bounds and left bounds equals right
while t <= b && l <= r
if dir == 0
(l..r).each { |left_index| puts matrix[t][left_index] }
t += 1
end
if dir == 1
(t..b).each { |top_col_ind| puts matrix[top_col_ind][r] }
r -= 1
end
if dir == 2
r.downto(l) { |right_index| puts matrix[b][right_index] }
b -= 1
end
if dir == 3
b.downto(t) { |bot_col_index| puts matrix[bot_col_index][l] }
l += 1
end
dir = (dir + 1) % 4
end
end