-
Notifications
You must be signed in to change notification settings - Fork 0
/
d1.go
69 lines (63 loc) · 1.43 KB
/
d1.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
62
63
64
65
66
67
68
69
package main
import (
"math"
"slices"
"sort"
"strconv"
"strings"
)
func (*methods) D1P1(input string) string {
lines := strings.Split(input, "\n")
var left, right []int64
for _, line := range lines {
sides := strings.Split(line, " ")
if len(sides) != 2 {
continue
}
l, _ := strconv.ParseInt(sides[0], 10, 64)
r, _ := strconv.ParseInt(sides[1], 10, 64)
left = append(left, l)
right = append(right, r)
}
sort.Slice(left, func(i, j int) bool { return left[i] < left[j] })
sort.Slice(right, func(i, j int) bool { return right[i] < right[j] })
var distance int64
for i, l := range left {
r := right[i]
distance += int64(math.Abs(float64(l - r)))
}
return strconv.FormatInt(distance, 10)
}
func (*methods) D1P2(input string) string {
lines := strings.Split(input, "\n")
var left, right []int64
for _, line := range lines {
sides := strings.Split(line, " ")
if len(sides) != 2 {
continue
}
l, _ := strconv.ParseInt(sides[0], 10, 64)
r, _ := strconv.ParseInt(sides[1], 10, 64)
left = append(left, l)
right = append(right, r)
}
ld := make([]int64, len(left))
copy(ld, left)
sort.Slice(ld, func(i, j int) bool { return ld[i] < ld[j] })
ld = slices.Compact(ld)
m := make(map[int64]int)
for _, l := range ld {
c := m[l]
for _, r := range right {
if l == r {
c++
}
}
m[l] = c
}
var score int64
for _, l := range left {
score += l * int64(m[l])
}
return strconv.FormatInt(score, 10)
}