-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrover.rb
63 lines (58 loc) · 1.71 KB
/
rover.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
class Rover
attr_accessor :max_size_of_plateau, :initial_position_of_rover, :current_position_of_rover
@@directionHash = {
:N_L => "W",
:W_L => "S",
:S_L => "E",
:E_L => "N",
:N_R => "E",
:W_R => "N",
:S_R => "W",
:E_R => "S",
:N_M => "N",
:W_M => "W",
:S_M => "S",
:E_M => "E"
}
def initialize max_size_of_plateau, initial_position_of_rover
self.current_position_of_rover = initial_position_of_rover
end
def update_position instruction
self.update_direction instruction
if instruction == 'M'
self.move_rover instruction
end
end
def move_rover_towards_north
self.current_position_of_rover[:y] = self.current_position_of_rover[:y] + 1
end
def move_rover_towards_south
self.current_position_of_rover[:y] = self.current_position_of_rover[:y] - 1
end
def move_rover_towards_east
self.current_position_of_rover[:x] = self.current_position_of_rover[:x] + 1
end
def move_rover_towards_west
self.current_position_of_rover[:x] = self.current_position_of_rover[:x] - 1
end
def move_rover instruction
moveHash = {
"N" => "north",
"S" => "south",
"W" => "west",
"E" => "east"
}
self.send("move_rover_towards_#{moveHash[self.current_position_of_rover[:direction]]}")
end
def update_direction instruction
direction = @@directionHash["#{self.current_position_of_rover[:direction]}_#{instruction}".to_sym]
self.current_position_of_rover[:direction] = direction
end
def calculate_final_position(instruction_set)
instruction_set = instruction_set.split(//)
instruction_set.each do |instruction|
self.update_position instruction
end
self.current_position_of_rover
end
end