aoc2024/day-05/internal/one/solve.go

28 lines
429 B
Go

package one
import (
"bytes"
"slices"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
updates, orderings, err := parseLines(r)
if err != nil {
return 0, err
}
var sum int
for _, update := range updates {
beforeSort := slices.Clone(update.Pages)
update.Sort(orderings)
if slices.Compare(beforeSort, update.Pages) == 0 {
sum += update.Pages[len(update.Pages)/2]
}
}
return sum, nil
}