Skip to content

Instantly share code, notes, and snippets.

@gingemonster
Last active December 1, 2016 08:08
Show Gist options
  • Save gingemonster/1486dc82b3b0f859f73a4c96eda41fd9 to your computer and use it in GitHub Desktop.
Save gingemonster/1486dc82b3b0f859f73a4c96eda41fd9 to your computer and use it in GitHub Desktop.
Advent of code 2016 day 1 nodejs solution
/*
* The Document indicates that you should start at the given coordinates (where you just landed) and face North. Then, follow the provided sequence: either turn left (L) or right (R) 90 degrees, then walk forward the given number of blocks, ending at a new intersection.
*
* There's no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the destination. Given that you can only walk on the street grid of the city, how far is the shortest path to the destination?
For example:
Following R2, L3 leaves you 2 blocks East and 3 blocks North, or 5 blocks away.
R2, R2, R2 leaves you 2 blocks due South of your starting position, which is 2 blocks away.
R5, L5, R5, R3 leaves you 12 blocks away.
How many blocks away is Easter Bunny HQ?
* */
var directions = "R1, R1, R3, R1, R1, L2, R5, L2, R5, R1, R4, L2, R3, L3, R4, L5, R4, R4, R1, L5, L4, R5, R3, L1, R4, R3, L2, L1, R3, L4, R3, L2, R5, R190, R3, R5, L5, L1, R54, L3, L4, L1, R4, R1, R3, L1, L1, R2, L2, R2, R5, L3, R4, R76, L3, R4, R191, R5, R5, L5, L4, L5, L3, R1, R3, R2, L2, L2, L4, L5, L4, R5, R4, R4, R2, R3, R4, L3, L2, R5, R3, L2, L1, R2, L3, R2, L1, L1, R1, L3, R5, L5, L1, L2, R5, R3, L3, R3, R5, R2, R5, R5, L5, L5, R2, L3, L5, L2, L1, R2, R2, L2, R2, L3, L2, R3, L5, R4, L4, L5, R3, L4, R1, R3, R2, R4, L2, L3, R2, L5, R5, R4, L2, R4, L1, L3, L1, L3, R1, R2, R1, L5, R5, R3, L3, L3, L2, R4, R2, L5, L1, L1, L5, L4, L1, L1, R1";
var directionsArray = directions.split(", ");
var currentCoordinates = [0 ,0];
var currentDirection = 0; // n=0, e=1, s=2, w=3
directionsArray.forEach(function(step) {
var turnDirection = step[0];
var distance = parseInt(step.substring(1)); // get the rest
currentDirection = getNewDirection(turnDirection, currentDirection);
currentCoordinates = calculateNewCoordinates(currentCoordinates, currentDirection, distance);
});
console.log("Moved North: " + currentCoordinates[0]);
console.log("Moved East: " + currentCoordinates[1]);
console.log("Total Blocks Away: " + (Math.abs(currentCoordinates[0]) + Math.abs(currentCoordinates[1])));
function getNewDirection(turnDirection, currentDirection){
// n=0, e=1, s=2, w=3
var changeBy = turnDirection == "L" ? -1 : 1;
var newDirection = currentDirection + changeBy;
if(newDirection==-1) newDirection = 3;
if(newDirection==4) newDirection = 0;
return newDirection;
}
function calculateNewCoordinates(currentCoordinates,newDirection, distance){
var newCoordinates = null;
switch(newDirection){
case 0:
// north
newCoordinates = [currentCoordinates[0] + distance, currentCoordinates[1]];
break;
case 1:
// east
newCoordinates = [currentCoordinates[0], currentCoordinates[1] + distance];
break;
case 2:
// south
newCoordinates = [currentCoordinates[0] - distance, currentCoordinates[1]];
break;
case 3:
// north
newCoordinates = [currentCoordinates[0], currentCoordinates[1] - distance];
break;
}
return newCoordinates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment