Advent of Code Day 1 - "No Time for a Taxicab"

Advent of Code is a fun competition. Here is a link to the first day. Each day has two parts.

--- Day 1: No Time for a Taxicab ---

Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you to retrieve all fifty stars by December 25th.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

You're airdropped near Easter Bunny Headquarters in a city somewhere. "Near", unfortunately, is as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody had time to work them out further.

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?

This is my rusty take on Advent of Code Day 1 in JS:

    str = "R2, L1, R2, R1, R1, L3, R3, L5, L5, L2, L1, R4, R1, R3, L5, L5, R3, L4, L4, R5, R4, R3, L1, L2, R5, R4, L2, R1, R4, R4, L2, L1, L1, R190, R3, L4, R52, R5, R3, L5, R3, R2, R1, L5, L5, L4, R2, L3, R3, L1, L3, R5, L3, L4, R3, R77, R3, L2, R189, R4, R2, L2, R2, L1, R5, R4, R4, R2, L2, L2, L5, L1, R1, R2, L3, L4, L5, R1, L1, L2, L2, R2, L3, R3, L4, L1, L5, L4, L4, R3, R5, L2, R4, R5, R3, L2, L2, L4, L2, R2, L5, L4, R3, R1, L2, R2, R4, L1, L4, L4, L2, R2, L4, L1, L1, R4, L1, L3, L2, L2, L5, R5, R2, R5, L1, L5, R2, R4, R4, L2, R5, L5, R5, R5, L4, R2, R1, R1, R3, L3, L3, L4, L3, L2, L2, L2, R2, L1, L3, R2, R5, R5, L4, R3, L3, L4, R2, L5, R5";
vals = str.split(", ");
var init = [0, 0];
var orientation = [1, 1];
var firstFacing = 0;

for (x = 0; x < vals.length; x++) {

k = vals[x];
direction = k[0];
amount = k.slice(1);

// So, think of the city blocks as an (X,Y) plane. You begin at (0,0) (init)
// facing North. Treat "Right" as the scalar value 1. Then each move is:
// determining if you're in a "right-hand" orientation (North or West) i.e.
// turning right will increase X or Y or a left-hand orientation (South or East).
// Then update your coordinate position and new orientation accordingly.

firstFacing = 1 - firstFacing; // switch from NS to EW
currentOrientation = orientation[1 - firstFacing];
if (direction == "R") {
directionMagnitude = 1;
} else {
directionMagnitude = -1;
}
finalDirection = directionMagnitude * currentOrientation;

init[firstFacing] += amount * finalDirection;

// That weird -2 * firstFacing + 1 formula is to address the directional quadrants:
// Basically orientation 0 = NS, -2 * 0 + 1 = 1, so turns retain their magnitude (right = +X)
// orientation 1 = EW, -2 * 1 + 1 = -1, so turns invert (right = -Y)
// So this works out if you end up facing East, a right-turn means -Y (heading South.)
orientation[firstFacing] = finalDirection * (-2 * firstFacing + 1);
};

var answer = Math.abs(init[0]) + Math.abs(init[1]);


What I'd really like is someone to point me to some of the ES6 functional programming style, or really anything "modern" in JS, for further reading and instruction.

• It would help if you posted what the goal of "Advent of Code Day 1" means. Perhaps include the requirements in the question, otherwise it is difficult to know what the point of this code is. Dec 5, 2016 at 16:51
• Furthermore, there's a different "Advent of Code Day 1" every year. Dec 5, 2016 at 17:30
• Hey guys, thanks for the feedback, I posted this kind of haphazardly as I was heading out to work, I should've spent a bit more time on it, my apologies. Dec 7, 2016 at 18:25

From my review:

• Declare all your variables with var
• Since you never use x except for determining k, you can use for( k of vals) though you could of course also type (for val of vals), k is kind of meaningless, or even use s for string.
• You could also consider Array.prototype.forEach instead..
• This

if (direction == "R") {
directionMagnitude = 1;
} else {
directionMagnitude = -1;
}


could be

directionMagnitude = (direction == "R") ? 1 : -1;


I like your approach a lot, though I still don't get really how you do the turning still. If this were production code, I would expect a bit more commenting there. All in all, cool!