-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (52 loc) · 768 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
60
61
package main
import (
"fmt"
"slices"
"strings"
"github.com/devkvlt/aoc"
)
var (
n int
left []int
right []int
)
func init() {
lines := aoc.Lines("input")
n = len(lines)
left = make([]int, n)
right = make([]int, n)
for i, line := range lines {
f := strings.Fields(line)
left[i] = aoc.Atoi(f[0])
right[i] = aoc.Atoi(f[1])
}
}
func part1() {
slices.Sort(left)
slices.Sort(right)
sum := 0
for i := 0; i < n; i++ {
sum += aoc.Abs(left[i] - right[i])
}
fmt.Println(sum)
}
func freq(x int, s []int) int {
f := 0
for _, y := range s {
if x == y {
f++
}
}
return f
}
func part2() {
sum := 0
for _, x := range left {
sum += x * freq(x, right)
}
fmt.Println(sum)
}
func main() {
part1() // 2164381
part2() // 20719933
}