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

49 lines
787 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 y := range graph.data {
for x := range graph.data[y] {
current := newPoint(x, y)
if graph.valueAt(current) == 'X' {
for _, n := range neighbours(current) {
if checkNeighbours(n, "MAS", graph) {
sum++
}
}
}
}
}
return sum, nil
}
func checkNeighbours(n point, word string, g *graph) bool {
if len(word) == 0 {
log.Debug("we found a full XMAS")
return true
}
if g.isOutOfBounds(n) {
return false
}
if g.valueAt(n) != rune(word[0]) {
return false
}
return checkNeighbours(neighbours(n)[n.direction], word[1:], g)
}