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