Skip to content

Commit f912f88

Browse files
committed
feat: day 01 part 2 solution
1 parent 37bad2f commit f912f88

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

2024/01/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,30 @@ func solvePart1(input string) int {
3030
return totalDistance
3131
}
3232

33+
func solvePart2(input string) int {
34+
left, right, err := inputs.ExtractIntPairs(input)
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
rightCount := make(map[int]int)
40+
similarityScore := 0
41+
42+
for _, rnum := range right {
43+
rightCount[rnum]++
44+
}
45+
46+
for _, lnum := range left {
47+
similarityScore += lnum * rightCount[lnum]
48+
}
49+
50+
return similarityScore
51+
}
52+
3353
func main() {
3454
part1Solution := solvePart1(input)
55+
part2Solution := solvePart2(input)
3556

3657
fmt.Println("Day 01 Part 1 solution:", part1Solution)
58+
fmt.Println("Day 01 Part 2 solution:", part2Solution)
3759
}

2024/01/main_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ import (
99
var test1 string
1010

1111
func TestSolution(t *testing.T) {
12-
t.Run("Day 01 part 1 test 1", func(t *testing.T) {
12+
t.Run("Day 01 part 1", func(t *testing.T) {
1313
want := 11
1414
got := solvePart1(test1)
1515

1616
if got != want {
1717
t.Errorf("Incorrect solution, got %d want %d", got, want)
1818
}
1919
})
20+
21+
t.Run("Day 01 part 2", func(t *testing.T) {
22+
want := 31
23+
got := solvePart2(test1)
24+
25+
if got != want {
26+
t.Errorf("Incorrect solution, got %d want %d", got, want)
27+
}
28+
})
2029
}

0 commit comments

Comments
 (0)