Skip to content

Commit 51fb1c9

Browse files
committed
2024 Day 1: Historian Hysteria; part 2
1 parent 37b059a commit 51fb1c9

File tree

4 files changed

+86
-8
lines changed

4 files changed

+86
-8
lines changed

2024/day-01/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,55 @@ distances between all of the pairs you found. In the example above, this is `2 +
8080

8181
Your actual left and right lists contain many location IDs. What is the total
8282
distance between your lists?
83+
84+
## Part Two
85+
86+
Your analysis only confirmed what everyone feared: the two lists of location IDs
87+
are indeed very different.
88+
89+
Or are they?
90+
91+
The Historians can't agree on which group made the mistakes or how to read most
92+
of the Chief's handwriting, but in the commotion you notice an interesting
93+
detail: a lot of location IDs appear in both lists! Maybe the other numbers
94+
aren't location IDs at all but rather misinterpreted handwriting.
95+
96+
This time, you'll need to figure out exactly how often each number from the left
97+
list appears in the right list. Calculate a total similarity score by adding up
98+
each number in the left list after multiplying it by the number of times that
99+
number appears in the right list.
100+
101+
Here are the same example lists again:
102+
103+
```
104+
3 4
105+
4 3
106+
2 5
107+
1 3
108+
3 9
109+
3 3
110+
```
111+
112+
For these example lists, here is the process of finding the similarity score:
113+
114+
- The first number in the left list is `3`. It appears in the right list three
115+
times, so the similarity score increases by `3 * 3 = 9`.
116+
117+
- The second number in the left list is `4`. It appears in the right list once,
118+
so the similarity score increases by `4 * 1 = 4`.
119+
120+
- The third number in the left list is `2`. It does not appear in the right
121+
list, so the similarity score does not increase (`2 * 0 = 0`).
122+
123+
- The fourth number, `1`, also does not appear in the right list.
124+
125+
- The fifth number, `3`, appears in the right list three times; the similarity
126+
score increases by `9`.
127+
128+
- The last number, `3`, appears in the right list three times; the similarity
129+
score again increases by `9`.
130+
131+
So, for these example lists, the similarity score at the end of this process is
132+
`31` (`9 + 4 + 0 + 0 + 9 + 9`).
133+
134+
Once again consider your left and right lists. What is their similarity score?

2024/day-01/main.go

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func main() {
1515
}
1616

1717
fmt.Printf("Part 1: %d\n", part1(input))
18+
fmt.Printf("Part 2: %d\n", part2(input))
1819
}
1920

2021
func part1(input []string) int {
@@ -32,6 +33,24 @@ func part1(input []string) int {
3233
return sum
3334
}
3435

36+
func part2(input []string) int {
37+
list1, list2 := parse(input)
38+
39+
freq := make(map[int]int)
40+
41+
for n := range list2 {
42+
freq[list2[n]]++
43+
}
44+
45+
var sum int
46+
47+
for n := range list1 {
48+
sum += list1[n] * freq[list1[n]]
49+
}
50+
51+
return sum
52+
}
53+
3554
func parse(input []string) (list1, list2 []int) {
3655
for _, line := range input {
3756
var n1, n2 int

2024/day-01/main_test.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ package main
22

33
import "testing"
44

5+
var input = []string{
6+
"3 4",
7+
"4 3",
8+
"2 5",
9+
"1 3",
10+
"3 9",
11+
"3 3",
12+
}
13+
514
func TestPart1(t *testing.T) {
6-
input := []string{
7-
"3 4",
8-
"4 3",
9-
"2 5",
10-
"1 3",
11-
"3 9",
12-
"3 3",
15+
if got, want := part1(input), 11; got != want {
16+
t.Errorf("got %d, want %d", got, want)
1317
}
18+
}
1419

15-
if got, want := part1(input), 11; got != want {
20+
func TestPart2(t *testing.T) {
21+
if got, want := part2(input), 31; got != want {
1622
t.Errorf("got %d, want %d", got, want)
1723
}
1824
}

2024/day-01/output

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Part 1: 2904518
2+
Part 2: 18650129

0 commit comments

Comments
 (0)