2024-12-01 18:43:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"aoc2024/helper"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
var list_a, list_b []int
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
list_a, list_b = helper.ReadLinesTwoIntSlices()
|
|
|
|
|
|
|
|
// part 1
|
|
|
|
distance := list_distance(list_a, list_b)
|
|
|
|
fmt.Printf("Distance: %v\n", distance)
|
|
|
|
|
|
|
|
// part 2
|
|
|
|
similarity := list_similarity(list_a, list_b)
|
|
|
|
fmt.Printf("Similarity: %v\n", similarity)
|
|
|
|
}
|
|
|
|
|
|
|
|
func list_distance(list_a []int, list_b []int) (dist int) {
|
|
|
|
sort.Ints(list_a)
|
|
|
|
sort.Ints(list_b)
|
|
|
|
|
|
|
|
for i := range list_a {
|
2024-12-02 12:30:04 +00:00
|
|
|
dist += helper.Abs(list_a[i] - list_b[i])
|
2024-12-01 18:43:58 +00:00
|
|
|
}
|
|
|
|
return dist
|
|
|
|
}
|
|
|
|
|
|
|
|
func list_similarity(list_a []int, list_b []int) (sim int) {
|
|
|
|
for _, val := range list_a {
|
|
|
|
valcount := countIntInList(list_b, val)
|
|
|
|
sim += valcount * val
|
|
|
|
}
|
|
|
|
return sim
|
|
|
|
}
|
|
|
|
|
|
|
|
func countIntInList(lst []int, i int) (count int) {
|
|
|
|
for _, val := range lst {
|
|
|
|
if val == i {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|