update day1
nicer struct names
This commit is contained in:
parent
999a4a8fa2
commit
02b95a7e65
1 changed files with 13 additions and 10 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue