Skip to content

Commit e163a34

Browse files
committed
AoC 2024, Day 1: Solve Part 2
1 parent 9230863 commit e163a34

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: go/2024/day01.go

+15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
func main() {
1414
left, right := parse()
1515
fmt.Println(part1(left, right))
16+
fmt.Println(part2(left, right))
1617
}
1718

1819
func parse() ([]int, []int) {
@@ -60,3 +61,17 @@ func part1(left []int, right []int) int {
6061

6162
return int(sum)
6263
}
64+
65+
func part2(left []int, right []int) int {
66+
var counts = make(map[int]int)
67+
for _, val := range right {
68+
counts[val] = counts[val] + 1
69+
}
70+
71+
var sum = 0
72+
for _, val := range left {
73+
sum += val * counts[val]
74+
}
75+
76+
return sum
77+
}

0 commit comments

Comments
 (0)