Last active
August 29, 2015 14:22
-
-
Save stephendeyoung/a68cd12c3073ce919d6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is an answer to https://gist.github.com/stephendeyoung/6f38ef7664693f040a98 | |
var util = require('util'); | |
process.stdin.setEncoding('utf8'); | |
process.stdin.on('readable', function(chunk) { | |
var chunk = process.stdin.read(); | |
var data = chunk.split('\n'); | |
var mapCoords = data[0].split(' ').map(convertStringToNumber); | |
// formats the remaining input into the following data structure: | |
/* [ [ '1 1 E', 'RFRFRFRF' ], | |
[ '3 2 N', 'FRRFLLFFRRFLL' ], | |
[ '0 3 W', 'LLFFFLFLFL' ] ] */ | |
var inputs = extractInput(data.slice(1).filter(function(input) { | |
return input; | |
}), []); | |
inputs.forEach(function(input) { | |
var robotData = input[0].split(' '); | |
var robotPosition = getRobotCoords(robotData).map(convertStringToNumber).concat(robotData[2]); | |
var robotInstructions = input[1].split(''); | |
var finalRobotPosition = processRobotInstructions(mapCoords, robotPosition, robotInstructions); | |
var finalPositionToString = util.format('%s %s %s ', finalRobotPosition[0], finalRobotPosition[1], finalRobotPosition[2]) + (finalRobotPosition[3] ? finalRobotPosition[3] : '') + '\n'; | |
process.stdout.write(finalPositionToString); | |
}); | |
}); | |
function extractInput(data, newDataStructure) { | |
if (data.length) { | |
return extractInput(data.slice(2), newDataStructure.concat([[data[0], data[1]]])); | |
} else { | |
return newDataStructure; | |
} | |
} | |
function convertStringToNumber(string) { | |
return parseInt(string, 10); | |
} | |
function getRobotCoords(robotPosition) { | |
return robotPosition.slice(0, 2); | |
} | |
var lostRobotCoords = []; | |
function processRobotInstructions(mapCoords, robotPosition, robotInstructions) { | |
if (robotInstructions.length) { | |
var instruction = robotInstructions[0]; | |
var newRobotPosition; | |
if (instruction === 'R' || instruction === 'L') { | |
newRobotPosition = getRobotCoords(robotPosition).concat(updateRobotOrientation(instruction, robotPosition[2]), robotPosition.slice(3)); | |
} else if (instruction === 'F') { | |
var newRobotPositionTemp = moveRobot(robotPosition); | |
var robotCoords = getRobotCoords(newRobotPositionTemp); | |
var hasRobotBeenLostHerePrev = lostRobotCoords.some(function(coords) { | |
if (coords[0] === newRobotPositionTemp[0] && coords[1] === newRobotPositionTemp[1]) { | |
return true; | |
} | |
}); | |
if (hasRobotBeenLostHerePrev) { | |
newRobotPosition = robotPosition; | |
} else { | |
var invalidXCoord = newRobotPositionTemp[0] < 0 || newRobotPositionTemp[0] > mapCoords[0]; | |
var invalidYCoord = newRobotPositionTemp[1] < 0 || newRobotPositionTemp[1] > mapCoords[1]; | |
if (invalidXCoord || invalidYCoord) { | |
newRobotPosition = newRobotPositionTemp.concat('LOST'); | |
lostRobotCoords.push(getRobotCoords(newRobotPosition)); | |
} else { | |
newRobotPosition = newRobotPositionTemp; | |
} | |
} | |
} else { | |
console.log('Unknown command type. Ignoring and moving onto the next command'); | |
newRobotPosition = robotPosition; | |
} | |
return processRobotInstructions(mapCoords, newRobotPosition, robotInstructions.slice(1)); | |
} else { | |
return robotPosition; | |
} | |
} | |
var robotOrientations = { | |
R: { | |
N: 'E', | |
E: 'S', | |
S: 'W', | |
W: 'N' | |
}, | |
L: { | |
N: 'W', | |
E: 'N', | |
S: 'E', | |
W: 'S' | |
} | |
}; | |
function updateRobotOrientation(instruction, currentOrientation) { | |
return robotOrientations[instruction][currentOrientation]; | |
} | |
function moveRobot(robotPosition) { | |
var robotOrientation = robotPosition[2]; | |
var newRobotCoords; | |
if (robotOrientation === 'N') { | |
newRobotCoords = [robotPosition[0], robotPosition[1] + 1]; | |
} else if (robotOrientation === 'E') { | |
newRobotCoords = [robotPosition[0] + 1, robotPosition[1]]; | |
} else if (robotOrientation === 'S') { | |
newRobotCoords = [robotPosition[0], robotPosition[1] - 1]; | |
} else { | |
newRobotCoords = [robotPosition[0] - 1, robotPosition[1]]; | |
} | |
return newRobotCoords.concat(robotPosition.slice(2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment