Created
December 1, 2016 13:39
-
-
Save gingemonster/218ad254ef5cb02b5a8f96050c13aac5 to your computer and use it in GitHub Desktop.
Advent of code 2016 day 1-2 nodejs solution
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
/* | |
* 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 | |
var previousCoordinates = []; | |
previousCoordinates.push(currentCoordinates.toString()); | |
directionsArray.some(function(step) { | |
var turnDirection = step[0]; | |
var distance = parseInt(step.substring(1)); // get the rest | |
currentDirection = getNewDirection(turnDirection, currentDirection); | |
var result = move(currentCoordinates, currentDirection, distance, previousCoordinates); | |
currentCoordinates = result.newCoordinates; | |
if(result.repeatedCoordinates){ | |
console.log(previousCoordinates); | |
console.log("First Repeat North: " + result.repeatedCoordinates[0]); | |
console.log("First Repeat East: " + result.repeatedCoordinates[1]); | |
console.log("Total Blocks Away: " + (Math.abs(result.repeatedCoordinates[0]) + Math.abs(result.repeatedCoordinates[1]))); | |
return true; | |
} | |
}); | |
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 move(currentCoordinates,newDirection, distance, previousCoordinates){ | |
var result = null; | |
switch(newDirection){ | |
case 0: | |
// north | |
result = calculateNewCoordinates(previousCoordinates, currentCoordinates, distance, 0); | |
break; | |
case 1: | |
// east | |
result = calculateNewCoordinates(previousCoordinates, currentCoordinates, 0, distance); | |
break; | |
case 2: | |
// south | |
result = calculateNewCoordinates(previousCoordinates, currentCoordinates, -distance, 0); | |
break; | |
case 3: | |
// west | |
result = calculateNewCoordinates(previousCoordinates, currentCoordinates, 0, -distance); | |
break; | |
} | |
return result; | |
} | |
function calculateNewCoordinates(previousCoordinates, currentCoordinates, distanceN, distanceE){ | |
var distance = distanceN != 0 ? distanceN : distanceE; | |
var tempCoordinates; | |
var repeatedCoordinates; | |
for(var i=1;i<=Math.abs(distance); i++){ | |
var originali = distance > 0 ? i : -i; | |
tempCoordinates = distanceN != 0 ? [currentCoordinates[0] + originali, currentCoordinates[1]] : [currentCoordinates[0], currentCoordinates[1] + originali]; | |
if(previousCoordinates.includes(tempCoordinates.toString())){ | |
repeatedCoordinates = tempCoordinates; | |
break; | |
} | |
previousCoordinates.push(tempCoordinates.toString()); | |
} | |
return { newCoordinates: tempCoordinates, repeatedCoordinates: repeatedCoordinates}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment