aoc2024/day-04/internal/two/solve.go
2024-12-06 19:57:07 +00:00

54 lines
921 B
Go

package two
import (
"bytes"
"slices"
"github.com/onyx-and-iris/aoc2024/day-04/internal/util"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
lines, err := util.ReadLines(r)
if err != nil {
return 0, err
}
var sum int
for i := 0; i < len(lines); i++ {
for j := 0; j < len(lines[i]); j++ {
if lines[i][j] == 'A' {
neighbours := newNeighbours(j, i)
if func() bool {
for _, n := range neighbours.all() {
if n.outOfBounds(lines) {
return true
}
if !slices.Contains([]rune{'M', 'S'}, n.value(lines)) {
return true
}
}
return false
}() {
continue
}
matrix := newMatrix(
rune(lines[i][j]),
neighbours.NW.value(lines),
neighbours.NE.value(lines),
neighbours.SE.value(lines),
neighbours.SW.value(lines),
)
if matrix.validate() {
sum++
}
}
}
}
return sum, nil
}