2024-12-01 17:50:02 +00:00
|
|
|
package one
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"math"
|
2024-12-02 18:28:28 +00:00
|
|
|
"strings"
|
2024-12-01 17:50:02 +00:00
|
|
|
)
|
|
|
|
|
2024-12-06 19:57:07 +00:00
|
|
|
func Solve(buf []byte) (int, error) {
|
|
|
|
r := bytes.NewReader(buf)
|
2024-12-01 17:50:02 +00:00
|
|
|
left, right, err := parseLines(r)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var sum int
|
|
|
|
for i, n := range left {
|
|
|
|
sum += int(math.Abs(float64(n - right[i])))
|
|
|
|
}
|
|
|
|
return sum, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLines(r io.Reader) ([]int, []int, error) {
|
|
|
|
var left, right []int
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
2024-12-02 18:28:28 +00:00
|
|
|
nums := strings.Fields(scanner.Text())
|
2024-12-01 22:04:49 +00:00
|
|
|
|
2025-01-07 17:18:38 +00:00
|
|
|
left = insertSorted(left, mustConv(nums[0]))
|
|
|
|
right = insertSorted(right, mustConv(nums[1]))
|
2024-12-01 17:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return left, right, nil
|
|
|
|
}
|