Mars Roven problem

Mars Rover problem with JAVA solution
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 is represented by a combination of an x and y co-ordinates and a letter representing one of the four cardinal compass points. 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 first line of input is the upper-right coordinates of the plateau, the lower-left coordinates are assumed to be 0,0.

The rest of the input is information pertaining to the rovers that have been deployed. Each rover has two lines of input. The first line gives the rover’s position, and the second line is a series of instructions telling the rover how to explore the plateau.

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.

Each rover will be finished sequentially, which means that the second rover won’t start to move until the first one has finished moving.

Output
The output for each rover should be its final co-ordinates and heading.

Test Input
5 5
1 2 N
LMLMLMLMM
3 3 E
MMRMMRMRRM

Expected Output
1 3 N
5 1 E

Solution
1. MarsRoverController.java
public static void init(){
ReadRoverNewConfigurations.readCurrentPosition();
MarsRoverManager.getInstance().sendToRover(); // Send instruction

}

2. ReadRoverNewConfigurations.java
static Scanner userInputReader = new Scanner(System.in);
static String currentPosition = “”;
public static List<String> newPositions = new ArrayList<String>();

/**
* MarsRover current position
*/
public static void readCurrentPosition() {
System.out.println(“Enter current position”);
currentPosition = userInputReader.nextLine();
readMoveConfiguration();
}

/**
* Read MarsRover new position
*/
private static void readMoveConfiguration() {
System.out.println(“Enter first rover position : “);
newPositions.add(userInputReader.nextLine());
newPositions.add(userInputReader.nextLine());
System.out.println(“Enter second rover position : “);
newPositions.add(userInputReader.nextLine());
newPositions.add(userInputReader.nextLine());
}

3. MarsRoverManager
public static int X = 0;
public static int Y = 0;
public static String compass = “”;

/**
* Singleton object creation
* @return
*/
public static synchronized MarsRoverManager getInstance(){
return marsRoverManager;
}

/**
* Configure the input and send to Rover to move further.
*/
public void sendToRover() {
MarsRoverUtil.getNewDirection(ReadRoverNewConfigurations.newPositions.get(0));
startMovingProcess(X, Y, compass,
MarsRoverUtil.getNewDirectionPath(ReadRoverNewConfigurations.newPositions.get(1)));
MarsRoverUtil.getNewDirection(ReadRoverNewConfigurations.newPositions.get(2));
startMovingProcess(X, Y, compass,
MarsRoverUtil.getNewDirectionPath(ReadRoverNewConfigurations.newPositions.get(3)));
}

/**
* After instruction rover movement deciding method
* @param x
* @param y
* @param compass
* @param path
*/
private void startMovingProcess(int x, int y, String compass, char[] path) {
for(int move=0; move < path.length; move++) {
switch (path[move]) {
case ‘M’:
if(compass.equals(“N”) || compass.equals(“S”)) {
y = MarsRoverUtil.getFormulaConstantsValue(y, compass);
} else {
x = MarsRoverUtil.getFormulaConstantsValue(x, compass);
}
break;
case ‘R’:
compass = MarsRoverUtil.getNewCompass(compass+”R”);
break;
case ‘L’:
compass = MarsRoverUtil.getNewCompass(compass+”L”);
break;
}
}
System.out.println(x+” “+y+” “+compass);
}

4. MarsRoverConstants
    public static final String NR = “NR”;
public static final String E = “E”;
public static final String NL = “NL”;
public static final String W = “W”;
public static final String WR = “WR”;
public static final String N = “N”;
public static final String WL = “WL”;
public static final String S = “S”;
public static final String SR = “SR”;
public static final String SL =”SL”;
public static final String ER = “ER”;
public static final String EL = “EL”;

5. MarsRoverUtil
public static void getNewDirection(String directions){
if(directions.contains(” “)) {
String[] newDirection = Pattern.compile(” “).split(directions);
if(newDirection.length==3) {
MarsRoverManager.X = Integer.parseInt(newDirection[0].trim());
MarsRoverManager.Y = Integer.parseInt(newDirection[1].trim());
MarsRoverManager.compass = newDirection[2].trim();
} else {
System.out.println(“New direction input error”);
}
}
}

/**
* North Y+1 and South Y-1
* East X+1 and West X-1
*/
public static int getFormulaConstantsValue(int axisValue, String compass) {
switch (compass) {
case N:
return ++axisValue;
case E:
return ++axisValue;
case W:
return –axisValue;
case S:
return –axisValue;
}
return 0;
}

/**
* Get user instruction path
* @param jobToRover
* @return
*/
public static char[] getNewDirectionPath(String jobToRover) {
return jobToRover.toCharArray();
}

/**
* Find the new compass position
* @param compass
* @return
*/
public static String getNewCompass(String compass) {
switch (compass) {
case NR:
return E;
case NL:
return W;
case WR:
return N;
case WL:
return S;
case SR:
return W;
case SL:
return E;
case ER:
return S;
case EL:
return N;
}
return “”;
}