Advent of Code 2024 (Day 1)
Published on: Sunday, December 1, 2024 at 9:51 AM PST
Written by Josh Bright
Intro
Advent of Code is back again! This year i’m going to have a writeup of each day. I’ll be starting with Python but I will probably swing back around and implement the solutions in a few other languages.
Over the years ive been slowly changing how I setup code and data for these puzzles, so that it is easy to drop in the input files, and then create solutions with various languages. Each year I create a folder for the year i’m about to work on, and then I will create another folder for the language i’m using. This year i’m going to again go with having data folder as a sub folder of year folder so that it is easy to re-use that for each language. In the past I have had that data folder within each language for some reason or another.
In any case, for Python, I also include a common.py file that I can import into my solution files, mainly just as an easy way to get either the test input data, or the real data. This makes it easy to get things going each day, as well as being able to start with the test data, and then to the real data when i’m ready.
Here is my repo if anyone wants to take a look! Note: The creators of Advent of Code do not want people distributing the input files, so you will not find any of those in the repo, just my solutions.
Part 1
The first few days of AoC are usually pretty easy, and this one falls in that bucket. We really have 3 parts to this problem, each of which is a pretty normal thing you’d run across when tackling problems.
First off, we need to collect our data. This comes to us in a text file, with two numbers separated by some spaces. We just want the numbers, with the first number in one list, and the second number in another list.
Next, we need to sort those lists, once they have all the input data in them.
And the last part, we just need to compare the difference between each number in those sorted lists, and the total is our answer.
Part 2
For part 2, we still need to start with our two lists of data, but just not sorted. Part of this puzzle involves seeing how many times a number shows up in the other list. Luckily the Counter class in the collections package that is included with Python makes this part pretty easy. We hand it a list, and it gives us back a dictionary that we can use to see how many things are in that list we gave it.
To get this solution, we just need to multiply a number from one list, with how many times it shows up in the second list.
Now to wait for day 2!