1
0
Fork 0

update day1

nicer struct names
This commit is contained in:
Sebastian Mark 2024-12-03 20:23:59 +01:00
parent 999a4a8fa2
commit 02b95a7e65

View file

@ -8,13 +8,13 @@ import (
) )
type list struct { type list struct {
a []int left []int
b []int right []int
} }
func main() { func main() {
list_a, list_b := helper.ReadLinesTwoIntSlices() list_a, list_b := helper.ReadLinesTwoIntSlices()
list := list{a: list_a, b: list_b} list := list{left: list_a, right: list_b}
// part 1 // part 1
distance := list.distance() distance := list.distance()
@ -26,20 +26,22 @@ func main() {
} }
func (l list) distance() (dist int) { func (l list) distance() (dist int) {
sort.Ints(l.a) sort.Ints(l.left)
sort.Ints(l.b) sort.Ints(l.right)
for i := range l.a { for i := range l.left {
dist += helper.Abs(l.a[i] - l.b[i]) dist += helper.Abs(l.left[i] - l.right[i])
} }
return dist return dist
} }
func (l list) similarity() (sim int) { func (l list) similarity() (sim int) {
for _, val := range l.a { for _, lval := range l.left {
valcount := countIntInSlice(l.b, val) valcount := countIntInSlice(l.right, lval)
sim += valcount * val sim += lval * valcount
} }
return sim return sim
} }
@ -49,5 +51,6 @@ func countIntInSlice(lst []int, i int) (count int) {
count++ count++
} }
} }
return count return count
} }