2024(2): There is puzzle 2.

This commit is contained in:
2024-12-18 09:35:30 +01:00
parent 6d6b4bb8cb
commit c2c5a7f231
3 changed files with 57 additions and 1 deletions

View File

@ -2,7 +2,7 @@ import numpy as np
# Load both columns into numpy arrays of type int
left, right = np.loadtxt('input', dtype=int).T
left, right = np.loadtxt("input", dtype=int).T
# Sort both lists (ASC)
left.sort()

12
2024/1/2.py Normal file
View File

@ -0,0 +1,12 @@
import numpy as np
# Load both columns into numpy arrays of type int
left, right = np.loadtxt("input", dtype=int).T
similarity_score = 0
for l in left:
similarity_score += l*np.sum(right == l)
print(f"The similarity score is {similarity_score}.")

View File

@ -16,12 +16,14 @@ There's just one problem: by holding the two lists up side by side (your puzzle
For example:
```
3 4
4 3
2 5
1 3
3 9
3 3
```
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on.
@ -41,3 +43,45 @@ To find the total distance between the left list and the right list, add up the
Your actual left and right lists contain many location IDs. What is the total distance between your lists?
To begin, [get your puzzle input](input).
Your puzzle answer was `765748`.
The first half of this puzzle is complete! It provides one gold star: *
--- Part Two ---
Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different.
Or are they?
The Historians can't agree on which group made the mistakes or how to read most of the Chief's handwriting, but in the commotion you notice an interesting detail: a lot of location IDs appear in both lists! Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting.
This time, you'll need to figure out exactly 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.
Here are the same example lists again:
```
3 4
4 3
2 5
1 3
3 9
3 3
```
For these example lists, here is the process of finding the similarity score:
The first number in the left list is 3. It appears in the right list three times, so the similarity score increases by 3 * 3 = 9.
The second number in the left list is 4. It appears in the right list once, so the similarity score increases by 4 * 1 = 4.
The third number in the left list is 2. It does not appear in the right list, so the similarity score does not increase (2 * 0 = 0).
The fourth number, 1, also does not appear in the right list.
The fifth number, 3, appears in the right list three times; the similarity score increases by 9.
The last number, 3, appears in the right list three times; the similarity score again increases by 9.
So, for these example lists, the similarity score at the end of this process is 31 (9 + 4 + 0 + 0 + 9 + 9).
Once again consider your left and right lists. What is their similarity score?
Your puzzle answer was `27732508`.
Both parts of this puzzle are complete! They provide two gold stars: **