Skip to content

Instantly share code, notes, and snippets.

@claudinei-rod
Created April 14, 2022 18:27
Show Gist options
  • Save claudinei-rod/c28cc7ea7ed56978a0a079262d68abb5 to your computer and use it in GitHub Desktop.
Save claudinei-rod/c28cc7ea7ed56978a0a079262d68abb5 to your computer and use it in GitHub Desktop.
Mars Rover

Mars Rover

A squad of robotic rovers are to be landed by NASA on a plateau on Mars. This plateau, which is curiously rectangular, must be navigated by the rovers so that their on-board cameras can get a complete view of the surrounding terrain to send back to Earth.

A rover’s position and location is represented by a combination of x and y co-ordinates and a letter representing one of the four cardinal compass points (North, South, East, or West). The plateau is divided up into a grid to simplify navigation. An example position might be 0, 0, N, which means the rover is in the bottom left corner and facing North.

In order to control a rover, NASA sends a simple string of letters. The possible letters are ‘L’, ‘R’ and ‘M’. ‘L’ and ‘R’ makes the rover spin 90 degrees left or right respectively, without moving from its current spot. ‘M’ means move forward one grid point, and maintain the same heading.

Assume that the square directly North from (x, y) is (x, y+1). Assume that the square directly East from (x, y) is (x+1, y).

INPUT: The rover begins at position 0, 0 facing North, and accepts either L R or M as an input. Based on this input, a new set of coordinates is returned, as well as the current direction the rover is facing.

OUTPUT: The output for each rover should be its final co-ordinates and the direction.

i.e. The rover's current coordinates are (0, 4) and it is facing S

The above state could be achieved by the following sequence:

Input: M
Output: The rover's current coordinates are (0, 1) and it is facing N
Input: M
Output: The rover's current coordinates are (0, 2) and it is facing N
Input: M
Output: The rover's current coordinates are (0, 3) and it is facing N
Input: M
Output: The rover's current coordinates are (0, 4) and it is facing N
Input: L
Output: The rover's current coordinates are (0, 4) and it is facing W
Input: L
Output: The rover's current coordinates are (0, 4) and it is facing S

Challenge

Note: It would be helpful to read and understand each of these before starting.

  1. Design objects or functions to describe the Rover.
  2. Add functions/methods to support the command to move, and the command to change direction.
  3. Return the result, formatted as per the above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment