57 lines
1003 B
Go
57 lines
1003 B
Go
package day01
|
|
|
|
import (
|
|
"math"
|
|
"sort"
|
|
"strings"
|
|
|
|
"aoc2024/internal/common"
|
|
)
|
|
|
|
func Handle(input []string) (int, int) {
|
|
list1 := make([]int, len(input))
|
|
list2 := make([]int, len(input))
|
|
map1 := map[int]int{}
|
|
map2 := map[int]int{}
|
|
distance := 0
|
|
similariry := 0
|
|
|
|
for i, line := range input {
|
|
cols := strings.Split(line, " ")
|
|
list1[i] = common.StrToInt(cols[0])
|
|
list2[i] = common.StrToInt(cols[1])
|
|
}
|
|
|
|
sort.Slice(list1, func(i, j int) bool {
|
|
return list1[i] < list1[j]
|
|
})
|
|
|
|
sort.Slice(list2, func(i, j int) bool {
|
|
return list2[i] < list2[j]
|
|
})
|
|
|
|
for i, v := range list1 {
|
|
if _, ok := map1[v]; !ok {
|
|
map1[v] = 1
|
|
} else {
|
|
map1[v] += 1
|
|
}
|
|
if _, ok := map2[list2[i]]; !ok {
|
|
map2[list2[i]] = 1
|
|
} else {
|
|
map2[list2[i]] += 1
|
|
}
|
|
distance = distance + int((math.Abs(float64(v - list2[i]))))
|
|
}
|
|
|
|
for k, v := range map1 {
|
|
map2Value := 0
|
|
if value, ok := map2[k]; ok {
|
|
map2Value = value
|
|
}
|
|
similariry += (k * map2Value) * v
|
|
}
|
|
|
|
return distance, similariry
|
|
}
|