Sebastian Mark
999a4a8fa2
- move `helper` package files to root `helper` directory
- update all helper imports
- rename `GetLines` to `getLines` for consistent naming convention
🤖
53 lines
797 B
Go
53 lines
797 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
"aoc/helper"
|
|
)
|
|
|
|
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
|
|
}
|