-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
55 lines (44 loc) · 910 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
package main
import (
"io"
"sort"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
a1 := make([]int, 0, len(lines))
a2 := make([]int, 0, len(lines))
for _, line := range lines {
del := aoc.NewDelimiter(line, " ")
ints := del.GetInts()
a1 = append(a1, ints[0])
a2 = append(a2, ints[1])
}
sort.Ints(a1)
sort.Ints(a2)
res := 0
for i := range a1 {
res += aoc.Abs(a1[i] - a2[i])
}
return res
}
func fs2(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
a1 := make([]int, 0, len(lines))
a2 := make([]int, 0, len(lines))
for _, line := range lines {
del := aoc.NewDelimiter(line, " ")
ints := del.GetInts()
a1 = append(a1, ints[0])
a2 = append(a2, ints[1])
}
count := make(map[int]int)
for _, v := range a2 {
count[v]++
}
res := 0
for _, v := range a1 {
res += v * count[v]
}
return res
}