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

54 lines
882 B
Go

package one
import (
"bytes"
log "github.com/sirupsen/logrus"
)
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) == 'X' {
for _, n := range neighbours(current) {
if graph.isOutOfBounds(n) {
continue
}
if checkNeighbours(graph, n, "MAS") {
sum++
}
}
}
}
}
return sum, nil
}
func checkNeighbours(graph *graph, n point, word string) bool {
if len(word) == 0 {
log.Debug("we found a full XMAS")
return true
}
if graph.isOutOfBounds(n) {
return false
}
if graph.valueAt(n) != rune(word[0]) {
return false
}
return checkNeighbours(graph, neighbours(n)[n.direction], word[1:])
}