From 3fbc9545663caee820201786243485f234db9220 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Mon, 2 Dec 2024 17:13:25 +0100 Subject: [PATCH] update day1 rewrite all functions to methods --- 2024/01/main.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/2024/01/main.go b/2024/01/main.go index 96adaad..baddb7b 100644 --- a/2024/01/main.go +++ b/2024/01/main.go @@ -6,39 +6,43 @@ import ( "sort" ) -var list_a, list_b []int +type list struct { + a []int + b []int +} func main() { - list_a, list_b = helper.ReadLinesTwoIntSlices() + list_a, list_b := helper.ReadLinesTwoIntSlices() + list := list{a: list_a, b: list_b} // part 1 - distance := list_distance(list_a, list_b) + distance := list.distance() fmt.Printf("Distance: %v\n", distance) // part 2 - similarity := list_similarity(list_a, list_b) + similarity := list.similarity() 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) +func (l list) distance() (dist int) { + sort.Ints(l.a) + sort.Ints(l.b) - for i := range list_a { - dist += helper.Abs(list_a[i] - list_b[i]) + for i := range l.a { + dist += helper.Abs(l.a[i] - l.b[i]) } return dist } -func list_similarity(list_a []int, list_b []int) (sim int) { - for _, val := range list_a { - valcount := countIntInList(list_b, val) +func (l list) similarity() (sim int) { + for _, val := range l.a { + valcount := countIntInSlice(l.b, val) sim += valcount * val } return sim } -func countIntInList(lst []int, i int) (count int) { +func countIntInSlice(lst []int, i int) (count int) { for _, val := range lst { if val == i { count++