AoC Day1 – No Time for a Taxicab

This is the puzzle for Day 1 of Advent Of Code 2016.  All of my code for this day can be found in my Day 1 Repository

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?

This is the input for the puzzle:

To solve this problem, I need to read in the instructions and follow each one:

In order to find the new orientation, I created a Turn method that keeps the orientation value from 0 to 3 representing directions North, East, South, and West:

And a method to move the current position the correct distance:

After following all of the instructions, the distance to the end can be calculated and displayed:

Then Part 2 of the problem:

Then, you notice the instructions continue on the back of the Recruiting Document. Easter Bunny HQ is actually at the first location you visit twice.

How many blocks away is the first location you visit twice?

For this part, I needed to keep track of which spaced had already been visited.  First I created a variable to hold the route traveled, and set the initial location to true:

Since I only wanted to mark visited spaces once per instruction, I created one method to mark horizontal lines visited and a different method to mark vertical lines:

I updated my Move method to call these and passed the route variable in:

Instead of having the program keep track of how many times the route had crossed itself, I just looked through the output for the first “Found a repeat!” statement and calculated the answer from there.

Leave a Reply

Your email address will not be published. Required fields are marked *