Skip to content

Instantly share code, notes, and snippets.

@UlisesGascon
Last active March 15, 2017 11:13
Show Gist options
  • Save UlisesGascon/39f1ddf4500ed07dc510885b41183491 to your computer and use it in GitHub Desktop.
Save UlisesGascon/39f1ddf4500ed07dc510885b41183491 to your computer and use it in GitHub Desktop.
NASA Rover Challenge

NASA Rover Challenge

Context

The purpose of this test is to enable you to demonstrate your proficiency in solving problems using software engineering tools and processes. Read the specification below and produce a solution.

Your solution should be in the form of completed code. The problem specified below requires a solution that receives input, does some processing and then returns some output.

Specification

A robotic rover is to be landed by NASA on a plateau on Mars. This plateau, which is curiously square, must be navigated by the rover so that its 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 coordinates 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 parameter is the size of the square (the lower-left coordinates are assumed to be 0,0).

The rest of parameters is information pertaining to the rover that has been deployed.

The second argument gives the rover's position, and the third is a series of instructions telling the rover how to explore the plateau. The position is made up of two integers and a letter, corresponding to the x and y coordinates and the rover's orientation.

Output:

The output should be the final coordinates and heading.

Example: function setup(5, “1 2 N”, [“L”, “M”, “L”, “M”, “L”, “M”, “L”, “M”, “M”]) Expected Output: “1 3 N”

/*
----- RESUMEN -----
Objetivo: Controlar el desplazamiento
del robot de la NASA en Marte
Datos:
- Coordenadas simplificadas x e y
- Coordenadas de posicionamiento que incluyen la dirección con (N, S, E, W)
- Ordenes de movimiento desde tierra:
- L y R (90º grados) derecha o izquierda
- M mueve el robot una cuadricula (y+1) (de [0,0] a [0,1])
Requerimientos de la función de navegación:
setup(
tamaño de la plataforma,
posición del rover (espacios no array ¬¬"),
instrucciones (array)
)
test:
setup(5, "1 2 N", ["L", "M", "L", "M", "L", "M", "L", "M", "M"]) === "1 3 N"
*/
function setup(dimensionPlataforma, posicion, instrucciones) {
function desplazamiento() {
switch (direccion) {
case 0:
validarDesplazamiento("suma", "Y");
break;
case 180:
validarDesplazamiento("resta", "Y");
break;
case 90:
validarDesplazamiento("suma", "X");
break;
case 270:
validarDesplazamiento("resta", "X");
break;
default:
console.warn("Error con el desplazamiento!");
}
}
function validarDesplazamiento(tipo, eje) {
if (tipo === "suma" && eje === "X" && (posicionX + 1) <= dimensionPlataforma) {
posicionX++;
} else if (tipo === "resta" && eje === "X" && (posicionX - 1) >= 0) {
posicionX--;
} else if (tipo === "suma" && eje === "Y" && (posicionY + 1) <= dimensionPlataforma) {
posicionY++;
} else if (tipo === "resta" && eje === "Y" && (posicionY - 1) >= 0) {
posicionY--;
} else {
console.warn("El Rover se caería de la plataforma!");
}
}
function cambiarDireccion(factor) {
if (factor === "L") {
direccion -= 90;
} else if (factor === "R") {
direccion += 90;
} else {
console.warn("Error con el cambio de dirección.\n No es un parámetro válido.\n Dato original:", direccion);
}
validarDireccion(direccion);
}
function validarDireccion(grados) {
if (typeof(grados) === "string") {
switch (grados) {
case "N":
direccion = 0;
break;
case "S":
direccion = 180;
break;
case "E":
direccion = 90;
break;
case "W":
direccion = 270;
break;
default:
console.warn("Error con los Grados.\n No es un parámetro válido.\n Dato original:", grados);
}
} else if (typeof(grados) === "number") {
if (direccion === 360) {
direccion = 0;
}
if (direccion === -90) {
direccion = 270;
}
} else {
console.warn("Error con los Grados.\n No es número ni cadena.\n Dato original:", grados);
}
}
function conversionGrados() {
switch (direccion) {
case 0:
return "N";
case 180:
return "S";
case 90:
return "E";
case 270:
return "W";
default:
console.warn("Error en conversión de grados.\n Fuera de rango.\n Dato original:", direccion);
}
}
console.log("Dimenasión de la plataforma:", dimensionPlataforma);
console.log("Posición del Rover:", posicion);
console.log("Instrucciones:", instrucciones);
var posicionDetalles = posicion.split(' ');
var posicionX = posicionDetalles[0];
var posicionY = posicionDetalles[1];
var direccion = posicionDetalles[2];
validarDireccion(direccion);
console.log("Posición del Rover en detalle:");
console.log("- Eje X:", posicionX);
console.log("- Eje Y:", posicionY);
console.log("- Orientación:", direccion);
instrucciones.forEach(function(item) {
if (item === "M") {
desplazamiento();
} else {
cambiarDireccion(item);
}
});
return posicionX + " " + posicionY + " " + conversionGrados();
}
console.log("Test pasado:", setup(5, "1 2 N", ["L", "M", "L", "M", "L", "M", "L", "M", "M"]) === "1 3 N");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment