mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 22:30:47 +00:00
28 lines
429 B
Go
28 lines
429 B
Go
package two
|
|
|
|
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
|
|
}
|