Skip to content

Commit

Permalink
We now have an algebra that can compute the current location.
Browse files Browse the repository at this point in the history
  • Loading branch information
hemalvarambhia committed Oct 25, 2023
1 parent a129abc commit e5d7241
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/mars_rover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ def initialize(starting_position:, direction:, map:)
end

def execute(commands)
commands.each do |command|
case command
when 'b'
@current_location = backwards(current_location: @current_location)
when 'f'
@current_location = forwards(current_location: @current_location)
when 'l'
@current_location = rotate_left(current_location: @current_location)
when 'r'
@current_location = rotate_right(current_location: @current_location)
@current_location =
commands.inject(@current_location) do |location, command|
case command
when 'b'
backwards(current_location: location)
when 'f'
forwards(current_location: location)
when 'l'
rotate_left(current_location: location)
when 'r'
rotate_right(current_location: location)
else
location
end
end
end
end

def current_position
Expand Down

0 comments on commit e5d7241

Please sign in to comment.