1
0
Fork 0
adventofcode/2024/01/main.go
Sebastian Mark 3fbc954566 update day1
rewrite all functions to methods
2024-12-02 18:19:53 +01:00

52 lines
800 B
Go

package main
import (
"aoc2024/helper"
"fmt"
"sort"
)
type list struct {
a []int
b []int
}
func main() {
list_a, list_b := helper.ReadLinesTwoIntSlices()
list := list{a: list_a, b: 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.a)
sort.Ints(l.b)
for i := range l.a {
dist += helper.Abs(l.a[i] - l.b[i])
}
return dist
}
func (l list) similarity() (sim int) {
for _, val := range l.a {
valcount := countIntInSlice(l.b, val)
sim += valcount * val
}
return sim
}
func countIntInSlice(lst []int, i int) (count int) {
for _, val := range lst {
if val == i {
count++
}
}
return count
}