Skip to content

Commit 35d9c16

Browse files
committed
feat: Solving day1 part two
1 parent 8362b5c commit 35d9c16

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

docs/day1.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,38 @@ In the example list above, the pairs and distances would be as follows:
4444

4545
To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is `2 + 1 + 0 + 1 + 2 + 5`, a total distance of `11`!
4646

47-
Your actual left and right lists contain many location IDs. What is the total distance between your lists?
47+
Your actual left and right lists contain many location IDs. What is the total distance between your lists?
48+
49+
### Part Two
50+
51+
Your analysis only confirmed what everyone feared: the two lists of location IDs are indeed very different.
52+
53+
Or are they?
54+
55+
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.
56+
57+
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.
58+
59+
Here are the same example lists again:
60+
61+
```txt
62+
3 4
63+
4 3
64+
2 5
65+
1 3
66+
3 9
67+
3 3
68+
```
69+
70+
For these example lists, here is the process of finding the similarity score:
71+
72+
* 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`.
73+
* 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`.
74+
* 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)`.
75+
* The fourth number, `1`, also does not appear in the right list.
76+
* The fifth number, `3`, appears in the right list three times; the similarity score increases by `9`.
77+
* The last number, `3`, appears in the right list three times; the similarity score again increases by `9`.
78+
79+
So, for these example lists, the similarity score at the end of this process is `31` `(9 + 4 + 0 + 0 + 9 + 9)`.
80+
81+
Once again consider your left and right lists. What is their similarity score?

src/day1/day1.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ func Solve(input string) uint {
1313
return solve(parseInput(input))
1414
}
1515

16+
func PartTwo(input string) uint {
17+
left, right := parseInput(input)
18+
occurances := make(map[uint]uint)
19+
for _, r := range right {
20+
occurances[r] += 1
21+
}
22+
var result uint = 0
23+
for _, l := range left {
24+
result += l * occurances[l]
25+
}
26+
return result
27+
}
28+
1629
func parseInput(input string) ([]uint, []uint) {
1730
var a, b []uint
1831
for _, line := range strings.Split(input, "\n") {
@@ -22,9 +35,9 @@ func parseInput(input string) ([]uint, []uint) {
2235
}
2336

2437
x, _ := strconv.Atoi(values[0])
25-
a = slices.Concat(a, []uint{uint(x)})
38+
a = append(a, uint(x))
2639
y, _ := strconv.Atoi(values[1])
27-
b = slices.Concat(b, []uint{uint(y)})
40+
b = append(b, uint(y))
2841
}
2942
return a, b
3043
}
@@ -34,7 +47,7 @@ func solve(a, b []uint) uint {
3447
slices.Sort(b)
3548
var sum uint = 0
3649
for i := range a {
37-
sum = sum + absDiffUint(a[i], b[i])
50+
sum += absDiffUint(a[i], b[i])
3851
}
3952
return sum
4053
}

src/day1/day1_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ func TestSample(t *testing.T) {
1616
t.Errorf("Calculated solution was not expected")
1717
}
1818
}
19+
20+
func TestSamplePartTwo(t *testing.T) {
21+
input := `3 4
22+
4 3
23+
2 5
24+
1 3
25+
3 9
26+
3 3`
27+
result := PartTwo(input)
28+
if result != 31 {
29+
t.Errorf("Calculated solution was not expected")
30+
}
31+
}

0 commit comments

Comments
 (0)