-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiral.rb
76 lines (65 loc) · 1.58 KB
/
spiral.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Spiral
def initialize(height, width, starting_point)
@height = height
@width = width
@matrix = build_matrix(@height, @width)
@starting_point = starting_point.split(',')
@output = []
@max_right = width - 1
@max_down = height - 1
@max_left = width - 1
@max_up = height - 1
end
def build_matrix(height, width)
[].tap do |matrix|
i = 1
height.times do
width_arr = []
width.times do
width_arr << i
i += 1
end
matrix << width_arr
end
end
end
def path
i = @starting_point[1].to_i
j = @starting_point[0].to_i
@output << @matrix[i][j]
while output_has_room?
current_right = 0
current_down = 0
current_left = 0
current_up = 0
while current_right < @max_right && output_has_room?
j += 1
@output << @matrix[i][j]
current_right += 1
end
@max_right -= 1
while current_down < @max_down && output_has_room?
i += 1
@output << @matrix[i][j]
current_down += 1
end
@max_down -= 2
while current_left < @max_left && output_has_room?
j -= 1
@output << @matrix[i][j]
current_left += 1
end
@max_left -= 2
@max_up -= 1
while current_up < @max_up && output_has_room?
i -= 1
@output << @matrix[i][j]
current_up += 1
end
end
@output.join(',')
end
def output_has_room?
@output.count < @height * @width
end
end