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

54 lines
893 B
Go
Raw Normal View History

2024-12-05 01:31:44 +00:00
package two
import (
"bytes"
"slices"
)
2024-12-06 19:57:07 +00:00
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
graph, err := readLines(r)
2024-12-05 01:31:44 +00:00
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' {
2024-12-05 01:31:44 +00:00
if func() bool {
for _, n := range neighbours(current) {
if graph.isOutOfBounds(n) {
2024-12-05 01:31:44 +00:00
return true
}
if !slices.Contains([]rune{'M', 'S'}, graph.valueAt(n)) {
2024-12-05 01:31:44 +00:00
return true
}
}
return false
}() {
continue
}
ns := neighbours(current)
2024-12-05 01:31:44 +00:00
matrix := newMatrix(
graph.valueAt(current),
graph.valueAt(ns[NW]),
graph.valueAt(ns[NE]),
graph.valueAt(ns[SE]),
graph.valueAt(ns[SW]),
2024-12-05 01:31:44 +00:00
)
if matrix.validate() {
sum++
}
}
}
}
return sum, nil
}