36 lines
610 B
Go
36 lines
610 B
Go
package day01
|
|
|
|
import (
|
|
"math"
|
|
"sort"
|
|
"strings"
|
|
|
|
"aoc2024/internal/common"
|
|
)
|
|
|
|
func Handle(input []string) int {
|
|
list1 := make([]int, len(input))
|
|
list2 := make([]int, len(input))
|
|
distance := 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 {
|
|
distance = distance + int((math.Abs(float64(v - list2[i]))))
|
|
}
|
|
|
|
return distance
|
|
}
|