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

44 lines
741 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)
data, err := util.ParseLines(r)
if err != nil {
return 0, err
}
var sum int
for i, products := range data[1] {
inOrder := true
for _, product := range products {
for _, rules := range data[0] {
if !slices.Contains(rules, product) {
continue
}
indx1, indx2 := slices.Index(products, rules[0]), slices.Index(products, rules[1])
if indx1 != -1 && indx2 != -1 && indx2 < indx1 {
inOrder = false
break
}
}
if !inOrder {
break
}
}
if inOrder {
sum += data[1][i][len(data[1][i])/2]
}
}
return sum, nil
}