-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (53 loc) · 1011 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"aoc/internal/utils"
_ "embed"
"fmt"
"slices"
"strings"
)
//go:embed input.txt
var input string
func partOne(left []int, right []int) int {
var sum int
for i := range left {
diff, _ := utils.AbsInt(left[i] - right[i])
sum += diff
}
return sum
}
func partTwo(left []int, right []int) int {
simScore := 0
cache := make(map[int]int) // number, occurrences
for _, num := range left {
occ, ok := cache[num]
if ok {
simScore += num * occ
continue
}
count := 0
for j := range right {
if num == right[j] {
count += 1
}
}
simScore += num * count
cache[num] = count
}
return simScore
}
func main() {
var left, right []int
for idx, strNum := range strings.Fields(input) {
number := utils.StrToInt(strNum)
if idx%2 == 0 {
left = append(left, number)
} else {
right = append(right, number)
}
}
slices.Sort(left)
slices.Sort(right)
fmt.Println("Part 1: ", partOne(left, right))
fmt.Println("Part 2: ", partTwo(left, right))
}