1
0
Fork 0
adventofcode/2024/01/main.go
Sebastian Mark 02b95a7e65 update day1
nicer struct names
2024-12-03 20:27:21 +01:00

56 lines
842 B
Go

package main
import (
"fmt"
"sort"
"aoc/helper"
)
type list struct {
left []int
right []int
}
func main() {
list_a, list_b := helper.ReadLinesTwoIntSlices()
list := list{left: list_a, right: list_b}
// part 1
distance := list.distance()
fmt.Printf("Distance: %v\n", distance)
// part 2
similarity := list.similarity()
fmt.Printf("Similarity: %v\n", similarity)
}
func (l list) distance() (dist int) {
sort.Ints(l.left)
sort.Ints(l.right)
for i := range l.left {
dist += helper.Abs(l.left[i] - l.right[i])
}
return dist
}
func (l list) similarity() (sim int) {
for _, lval := range l.left {
valcount := countIntInSlice(l.right, lval)
sim += lval * valcount
}
return sim
}
func countIntInSlice(lst []int, i int) (count int) {
for _, val := range lst {
if val == i {
count++
}
}
return count
}