Advent of Code 2024 day 1 strategy
Published
Advent of Code 2024 is here. Let's talk strategy for the first day's puzzle, Historian Hysteria. There is no single "correct" way to solve problems in this puzzle - it's all about getting to the correct output however you can. So keep in mind that the strategy guide below is just a suggestion, and you can make it more or less complicated, more or less optimized, or just better for your brain to understand.
Part 1: Total distance between lists
The input for this puzzle is a text file with two columns of numbers representing location IDs. This is what you need to do with the list:
-
Get the data into your programming environment.
- Import the file (harder), or
- Copy paste the file straight into your code editor (easier, but less like the programming you will do on the job)
-
When you have access to the data, split it up so that each line can be dealt with appropriately. For many programming environments, that will mean splitting the single big string you're imported on the line delimiter:
\n
-
Once you have an array of location IDs, each string (text) element in the array will look something like
12345 67890
. At this point, for each element in your array, you want to split each of these strings on the three spaces between them. This should leave you with a sub-array of two string elements like["12345", "67890"]
. If your data doesn't look like that, you should throw it away (ex: deal with the empty last line in your input file). -
While you're
for each
-ing, for each sub-array, push the element on the left into one array, and the element on the right into another array. This should leave you with two lists of equal length:- A list of the left-hand location IDs from your input file
- A list of the right-hand location IDs
-
Now that you have your two lists, remember that the instructions tell us we need to "pair up the smallest number in the first list" with the smallest number in the second, and so on. This means we need to sort our lists so they are arranged smallest to largest (or largest to smallest, as long as they're both sorted the same it doesn't matter in this case).
-
Loop through the first list, and with each element, find the difference between the element in the same position in the second list. We need to keep track of the "total distance", so make sure you are summing the absolute difference between these numbers, not just adding both positive and negative differences together.
-
You should now have a positive integer, unique to your data set, that represents the sum of the absolute differences between the location IDs in each list. This is your answer. Hopefully it's right!
Part 2: Similarity score
This one is super simple if you've made it through Part 1. We can start with the exact same steps until we get to 6:
-
This time when we loop through the first list, we just need to compare the element in a given position with the equivalently-positioned element in the second list. If they are the same, we need to increase a count by one.
-
The total count of identical location IDs is our "similarity score". This is your answer. You got this!