How to solve Advent of Code 2021 - Day 1 with Python

How to solve Advent of Code 2021 - Day 1 with Python

Before we get started:

What even IS Advent of Code??!!

Good question, my friend. Here is the about page: https://adventofcode.com/2021/about and they themselves say:

Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

So it's coding puzzles. They roughly follow some story of how you need to save Christmas by solving the puzzles and collecting 2 gold stars every day. You can solve the challenges whenever you want after they unlocked for the day and still get the 2 stars - I'm currently 2 days behind, because it's already Day 4 and I only solved Day 1 and 2 but that's no problem.

There is however a leader board where you can collect fame and points and you get on there by being one of the fastest people to solve the challenge and submit the correct answer. Currently these are still being filled within the first 10min of a day, so that just sounds like unnecessary stress to me *shrug*.

I just mentioned, there are 2 stars each day. That's because there are 2 parts of the puzzle each day. After you solved Part 1, you get one star and the second part unlocks.

Increasing difficulty

The first tasks are super easy and the leader board was filled within 3 minutes. However, I have been told that the puzzles get increasingly difficult as the days go on and can take even experienced coders multiple hours on the last days. But, as we know, difficulty is always subjective, so we will see how far I can get.

But if you're bored by this first task, don't worry. This is not how all of them will be.

My GitHub Repository for Advent Of Code

https://github.com/GalaxyInfernoCodes/Advent_Of_Code_2021

This is where I will upload all of my Python Notebooks where I solve the challenges. I'm solving them on Google Colab, because that allows me to just quickly throw something together in the Browser on any machine I'm on. Since I'm lazy, that's my go-to for quick and small projects. Otherwise I code in VSCode :)

Day 1 Puzzle

In the following I will describe the puzzle and show my solution, but if you're impatient and just want to see my code, you can go here: https://github.com/GalaxyInfernoCodes/Advent_Of_Code_2021/blob/main/AdventOfCode_Day1.ipynb
If you think that my code is shitty and inefficient, I don't care. I just wanted to solve the challenge, not win a beauty contest.

Here is the challenge, if you want to read the full puzzle: https://adventofcode.com/2021/day/1

Part 1

Day 1 gives you an input file with integers like this:

199
200
208
210
200
207
240
269
260
263

These represent sea floor depths in the story, because we are on a submarine.

First I copy-pasted the input numbers into a text file on my server and then read in the numbers into an array:

with open('input.txt', 'r') as f:
    lines = f.readlines()
    measurements = [int(entry.strip()) for entry in lines]

The goal of the first part was to track how quickly the depth increases, because we are chasing some keys and don't want them to get lost. Here the task was to simply count in how many of the steps the depths increases, so I solved that with a simple loop and compared the current entry with the previous one:

prev_entry = measurements[0]
increases = 0
for entry in measurements[1:]:
    if entry > prev_entry:
        increases += 1
    prev_entry = entry

The increases variable then holds the answer to the first challenge.

Part 2

Because the data was too noisy, in part 2 we are supposed to use a sliding window of length 3 and add the depths of each sliding window to a sum before then comparing the sums as previously done with the pure data.

To compute the sliding window, I used the numpy.convolve function as follows:

sliding_windows = np.convolve(measurements, np.ones(3), 'valid')

The resulting array is 2 entries shorter than the measurements array, because we at the last two entries we can't fit a whole window anymore.

Afterwards, I just use the same counting-loop again:

prev_entry = sliding_windows[0]
increases = 0
for entry in sliding_windows[1:]:
    if entry > prev_entry:
        increases += 1
    prev_entry = entry

And increases once again gives us the correct answer.

Conclusion

This was quite easy, but at least I had to remember how to read in a file and convert the lines into a numbers array - I don't often read files. And the sliding window approach using numpy is fairly new to me, so this was a good practice for my memory :)

Excited to see the rest of the challenges and how far I can get.