You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
800 B
53 lines
800 B
package adventoc2024
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"sort"
|
|
)
|
|
|
|
type Day1 struct{}
|
|
|
|
func (d Day1) Part1(r io.Reader) string {
|
|
s := bufio.NewScanner(r)
|
|
var sum int
|
|
var left, right []int
|
|
|
|
var lft, rgh int
|
|
for s.Scan() {
|
|
fmt.Sscanf(s.Text(), "%d %d", &lft, &rgh)
|
|
left, right = append(left, lft), append(right, rgh)
|
|
}
|
|
|
|
sort.Ints(left)
|
|
sort.Ints(right)
|
|
|
|
for i := range right {
|
|
sum += abs(left[i] - right[i])
|
|
}
|
|
|
|
return printi(sum)
|
|
}
|
|
|
|
func (d Day1) Part2(r io.Reader) string {
|
|
s := bufio.NewScanner(r)
|
|
var sum int
|
|
var left []int
|
|
right := make(map[int]int)
|
|
|
|
var lft, rgh int
|
|
for s.Scan() {
|
|
fmt.Sscanf(s.Text(), "%d %d", &lft, &rgh)
|
|
left = append(left, lft)
|
|
right[rgh]++
|
|
}
|
|
|
|
sort.Ints(left)
|
|
|
|
for i := range left {
|
|
sum += left[i] * right[left[i]]
|
|
}
|
|
|
|
return printi(sum)
|
|
}
|
|
|