Skip to content

Instantly share code, notes, and snippets.

@claudinei-rod
Last active January 24, 2023 14:58
Show Gist options
  • Save claudinei-rod/b2044e227e46b11abbbc8d0dd4d71f86 to your computer and use it in GitHub Desktop.
Save claudinei-rod/b2044e227e46b11abbbc8d0dd4d71f86 to your computer and use it in GitHub Desktop.
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).

INPUT: The rover begins at position 0, 0 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.

The position is made up of two integers and a letter separated by spaces, corresponding to the x and y co-ordinates and the rover’s orientation.

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.

  • You’re welcome to use an Object-Oriented approach or a Functional one.
  • Design a class or object to describe the Rover.
  • Add methods or functions to support the command to move, and the command to change direction.
  • Return the result, formatted as per the above

Stretch Goal 1: Add boundaries to the space that prevents movement outside of a certain set of coordinates

Stretch Goal 2: Add “boulders” in a couple of grid locations and have the Rovers avoid them.

Stretch Goal 3: Add an additional rover and alternate between Rovers for each input. Ensure that the Rovers do not overlap.

Stretch Goal 4: Verbally describe a design that would represent a Driver and talk through how you would have a Driver with a single Rover, cover every valid square in the grid. (Keeping in mind that there can still be boulders)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment