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

30 lines
492 B
Go
Raw Normal View History

2024-12-05 18:24:22 +00:00
package one
import (
"bytes"
"slices"
"github.com/onyx-and-iris/aoc2024/day-05/internal/util"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
updates, orderings, err := util.ParseLines(r)
2024-12-05 18:24:22 +00:00
if err != nil {
return 0, err
}
var sum int
for _, update := range updates {
beforeSort := slices.Clone(update.Pages)
update.Sort(orderings)
2024-12-05 18:24:22 +00:00
if slices.Compare(beforeSort, update.Pages) == 0 {
sum += update.Pages[len(update.Pages)/2]
2024-12-05 18:24:22 +00:00
}
}
return sum, nil
}