Advent of Code - Day 1

 read in about 4 minutes

The elves have discovered project management and this bit hit a little close to home.

The bad news is that they’ve realized they have a different emergency…

Both parts of this challenge are related to using the movements of a dial (with didgets 0-99). The movements are either to the left or right (L or R) and the number of places to move.

The example file I received looked like this:

L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

Part 1

This part simply asked to count the number of times you land exactly on zero. So once we make the movement check if the dial is on zero and increment the counter if we are. This is simple enough so most of the activity here is setup.

I participated last year using Python so I grabbed day 1 from last year so I could read a text file and pretty much everything was scrapped. Once I confirmed I could read the example I printed the results and started string parsing. Python makes this activity easy since you can use a string like an array. This is not the code I used but you can do the following:

move = str(line) # bring in the new line
direction = move[0] # capture the movement L or R
distance = int(move[1:99]) # capture the distance of the move

Then I translate the direction into a multiplier L=-1 and R=1. The distance has the multiplier applied and then is added to the current value of the dial.

Next we must determine the new value of the dial. To do this I used the following code. The % is modulo (mod), or remainder, in Python. This way the result of the dial is always between 0 and 99.

if dial < 0:
    dial = dial + 100
elif dial > 99:
    dial = dial % 100

Once the dial position is set I check if it is zero and increase the counter any time it is.

This method worked well for the example but did not return the correct result on the full import. This is the nature of these puzzles where the full input includes some cases that the example will not expose.

The issue was caused by some moves with distances greater than 100. This is actually okay in the positive direction but as you can see we’re adding 100 if the dial is < 0 so you will end up with large negative numbers if you move hundreds of places to the left.

Since rotationally moving 100 is like not moving at all I decided to mod the distance by 100 and only do the remainder distance.

distance = int(move[1:99])%100

This worked well and got the correct answer allowing me to make it to part 2 where Eric must have expected this exact solution and set it as a trap.

Part 2

Now we need to know how many times any click on the dial would be set to zero not just landing on zero. So immediately the shortcut from the first part of mod 100 on the distance is out or at the very least needs to be tweaked.

I moved the mod to later and inbetween added the whole number divisions to the running count of the dial hitting zero. In python int(a/b) will return the whole number of that division.

distance = int(move[1:99])
hits_zero += int(distance/100)
distance = distance%100

There was another edge case that I needed to accommodate which is if the dial starts on 0 prior to the move but that was easy enough to check. From there I was able to calculate the new value for the dial passing or landing on 0. In the world of the puzzle the elves can get in the door.

I’m glad this puzzle was so straightforward I know there are more challenging things to come.

Listening to: LCD Soundsystem - New York, I Love You but You're Bringing Me Down
Christopher Himes

I'm Christopher Himes (he⁠/⁠him), an accomplished tech professional living in Metro Detroit. I'm currently looking for work as a product owner or developer.

More about me

Interactions

@hachyderm.io/@mercutio

Comments

This blog uses a Mastodon and webmentions for comments. You can comment by replying on Mastodon/ActivityPub/Fediverse account or webmention.

Related Posts

Recent Posts