As far as online coding challenges go,
Advent of Code is one of the most entertaining. Problems can sometimes be answered without code, but using code can often make solutions much easier to discover. In this article, I walk through the steps required to answer the day one problem from AoC 2018 using
Advent of Code: An annual code challenge releasing one problem at midnight every night in December until xmas.
Each Advent of Code problem begins with a bit of story. The day one story in 2018 informs you that Santa’s Elves have detected “temporal anomalies”, so each night your successful completion of the challenge will help the elves get one step closer to saving xmas.
As we continue reading, tonight’s problem is explained. We must add a sequence of numbers. This is not a difficult problem, and not even a problem that knowledge of programming is required to solve. I’m still going to use
Python to solve it, which is a strategy that will pay off in the long run as this problem has a
part two that unlocks when part one is solved.
Part two is a bit more suited to the use of a programming language.
Click the link that says
get your puzzle input, or right-click and choose
save as, to obtain a text file with the problem input (name the file
input.txt). The text file is about
1000 lines with one number per line. Open the file up and have a look, we have to add all these numbers!
If you do feel like programming, create an empty text file called
sol.py in the same directory as
input.txt and add the following contents:
This code opens the file and obtains a list of all the lines in the file. The
for-loop part of the code loops over and prints each line in the file. Each line has a newline character (
\n) on the end; we could remove it, but this doesn’t concern us right now.
We’ve looped over all the lines. Now let’s add them. Replace the
for-loop with code that will add each line as an integer.
And that’s it! The code above prints the answer we need to the console. Enter the answer in the submission box to unlock
day one part two.
One might have some difficulty with part two if they don’t make the decision to use code. My code had to add approximately 136000 numbers before finding a solution. I don’t doubt its still possible to find a solution with a tool like Google Sheets, but that’s a lot of rows.
I chose to modify the above
Python solution to solve part two. The code below keeps track of every partial value we encounter and alerts us the first time we see a duplicate value.
In the above code, I call a function named
sol. I do this because my code now contains a
while loop and a
for-loop, and I want to be able to return an answer as soon as I find a duplicate frequency; the easiest way to directly return an answer is to
return from a function. Nevertheless, the code above loops over the list of numbers we were given as input and prints the
first frequency found twice.
If you had difficulty with this checkout the solution form on reddit: Day One Solutions. If you continue with AoC, there’s one held every year, and a global leaderboard is maintained showing the fastest solutions. I’m proud to say my 1:13 min solution got me into 53rd place on Day One!