mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
44 lines
741 B
Go
44 lines
741 B
Go
|
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
|
||
|
}
|