aoc2024/day-04/internal/two/solve.go

54 lines
893 B
Go

package two
import (
"bytes"
"slices"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
graph, err := readLines(r)
if err != nil {
return 0, err
}
var sum int
for i := 0; i < len(graph.data); i++ {
for j := 0; j < len(graph.data[i]); j++ {
current := newPoint(j, i)
if graph.valueAt(current) == 'A' {
if func() bool {
for _, n := range neighbours(current) {
if graph.isOutOfBounds(n) {
return true
}
if !slices.Contains([]rune{'M', 'S'}, graph.valueAt(n)) {
return true
}
}
return false
}() {
continue
}
ns := neighbours(current)
matrix := newMatrix(
graph.valueAt(current),
graph.valueAt(ns[NW]),
graph.valueAt(ns[NE]),
graph.valueAt(ns[SE]),
graph.valueAt(ns[SW]),
)
if matrix.validate() {
sum++
}
}
}
}
return sum, nil
}