🎄 Christmas Coding with Elixir, Day 1

Advent Of Code 2018

Santa: Big Elixir fan

Advent of Code is upon us again! I’m going to attempt to solve each puzzle with Elixir this year and highlight any interesting things I’ve learned.

The Puzzle: Part 1

After feeling like you’ve been falling for a few minutes, you look at the device’s tiny screen. “Error: Device must be calibrated before first use. Frequency drift detected. Cannot maintain destination lock.” Below the message, the device shows a sequence of changes in frequency (your puzzle input). A value like +6 means the current frequency increases by 6; a value like -3 means the current frequency decreases by 3.
For example, if the device displays frequency changes of +1, -2, +3, +1, then starting from a frequency of zero, the following changes would occur:
Current frequency 0, change of +1; resulting frequency 1.
Current frequency 1, change of -2; resulting frequency -1.
Current frequency -1, change of +3; resulting frequency 2.
Current frequency 2, change of +1; resulting frequency 3.
In this example, the resulting frequency is 3.
Here are other example situations:
+1, +1, +1 results in 3
+1, +1, -2 results in 0
-1, -2, -3 results in -6
Starting with a frequency of zero, what is the resulting frequency after all of the changes in frequency have been applied?

Solving It

So part 1 is pretty simple. We need to parse a list of positive and negative integers and then sum them together.

Here’s our input:

And here’s some code to get our answer. Let’s break it down line by line.

I like to be able to stream my puzzle inputs into my code, so we start with IO.read/2— this reads in from stdin so we can execute the code with

cat input.txt | ./advent1.1.exs

We pipe the string data into String.split/2, which gives us a list of strings in the form [“+16”, “-15”, “-2”, ...].

Next, we pipe this list into Enum.map/2 and pass it the String.to_integer/1 function, which will return a list of parsed integers in the form [16, -15, -2, ...].

Now all that remains is to call Enum.sum/1 to sum all those integers together, and a call to IO.inspect/1 to dump out the result.

$ cat input.txt | ./advent1.1.exs

The Puzzle: Part 2

You notice that the device repeats the same frequency change list over and over. To calibrate the device, you need to find the first frequency it reaches twice.
For example, using the same list of changes above, the device would loop as follows:
Current frequency 0, change of +1; resulting frequency 1.
Current frequency 1, change of -2; resulting frequency -1.
Current frequency -1, change of +3; resulting frequency 2.
Current frequency 2, change of +1; resulting frequency 3.
(At this point, the device continues from the start of the list.)
Current frequency 3, change of +1; resulting frequency 4.
Current frequency 4, change of -2; resulting frequency 2, which has already been seen.
In this example, the first frequency reached twice is 2. Note that your device might need to repeat its list of frequency changes many times before a duplicate frequency is found, and that duplicates might be found while in the middle of processing the list.
Here are other examples:
+1, -1 first reaches 0 twice.
+3, +3, +4, -2, -4 first reaches 10 twice.
-6, +3, +8, +5, -6 first reaches 5 twice.
+7, +7, -2, -7, -4 first reaches 14 twice.
What is the first frequency your device reaches twice?

Solving It

Ok! So now it gets a little trickier. In Ruby, I’d use Enumerable#cycle to give an infinite stream of frequencies and then use return to break out early when I found one that’d been seen before.

But Elixir is functional, and early returns are very much an imperative approach. So how to solve it?

Well, after a lot of digging around, I found Enum.reduce_while/3.

We start the solution in the same way as part 1, only this time we use Stream.cycle/1 to create an infinite stream of cycled frequencies. We pipe this stream into Enum.reduce_while/3, which takes an enumerable, an accumulator, and a reducer function.

The reducer function either returns a tuple with the first element as :cont and the second element as the updated accumulator, or it returns :halt as the first element and the end result as the second element.

Using this, we can maintain a set of frequencies seen before and test for membership — that’s pretty handy!

$ cat input.txt | ./advent1.2.exs


So that was fun, and a little trickier than I was expecting for day 1! I’ll be filing away Enum.reduce_while/3 for future reference 😁

I’ll be adding my Ruby and Elixir solutions to Github if you’d like to read further: https://github.com/seanhandley/adventofcode2018