Regular Memory

Advent of Code 2018: Day One

Posted at — Nov 30, 2019

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 Python.

Advent of Code: An annual code challenge releasing one problem at midnight every night in December until xmas.

Reading the Problem

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.

Downloading the Input File

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!

Read the Input File

If you don’t feel like programming yet, try pasting the input file into Google Sheets or Wolfram Alpha.

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:

1
2
3
4
lines = open("input.txt").readlines()

for line in lines:
    print(line)

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.

Writing a Solution

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.

1
2
3
4
5
6
7
lines = open("input.txt").readlines()

ans = 0
for line in lines:
    ans += int(line)

print(ans)

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.

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
lines = open("input.txt").readlines()

def sol():

    seen = {} # dictionary to track already seen answers
    ans = 0

    while True:

        # We might need to repeat the list before a duplicate
        # frequency is found
        for line in lines:
            ans += int(line)

            # Return first answer we see twice
            if ans in seen:
                return ans

            # Mark partial answer as seen
            seen[ans] = 1

ans = sol()
print(ans)

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.

Conclusion

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!

comments powered by Disqus