Metadata
-
Date
-
Tagged
-
Part of series
-
Older post
-
Newer post
Advent of Code 2024 Day 1
Day 1: Historian Hysteria
https://adventofcode.com/2024/day/1
The elvish chief historian is missing! Your task throughout this advent is finding him.
The elves decided to start looking in the historian’s office.
The elves split in two groups, each group compiles a list of places to look.
That todays input, the two lists of numbers.
- Each line has one number from each list.
An example input looks like this:
Parsing
Gettig the input into two lists of numbers.
Option 1: loopin’ with for
Keeping it simple. Loop through each line, get 2 numbers, push them each to a list.
Option 2: Iterators
Same idea as above, but turn each line into a tuple of two numbers first, then unzip
the iterator of tuples into two lists.
Part 1
The lists don’t look very similar, but maybe they are.
Pair op the smallest number in the left list with the smallest number in the right list. Calculate the absolute difference between those 2 numbers. Do this for all pairs in your input.
The question asks for the sum of all those absolute differences.
Option 1: Loopin’ with for
- Get the two lists
- Sort the two lists
- Calculate the absolute differences
- Add each difference to a running total before moving on to the next pair
Option 2: Iterators
Same idea as option 1, but taking advantage of zip
to turn the two lists into an iterator over pairs.
Finishing off with a sum
that sums each item in the iterator.
Part 2
Turns out the lists are very different after all. (Who would have thought? Not me)
A lot of the numbers appear in both lists, maybe that’s meaningful?
In part 2 you need to figure out how often each number from the left list appears in the right list.
Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list.
Option 1: Loopin’ with for
For each number in the left list, loop through the right list and count how many times you see the same number.
Option 2: Count occurrences
Count how many times each number appears in the right list first. Then calculate the product based on that result.