1
0
Fork 0
adventofcode/2024/01/main.go
2024-12-02 13:26:54 +01:00

56 lines
896 B
Go

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 {
dist += abs(list_a[i] - list_b[i])
}
return dist
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
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
}