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

49 lines
787 B
Go
Raw Normal View History

2024-12-05 01:31:44 +00:00
package one
import (
"bytes"
log "github.com/sirupsen/logrus"
)
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
}
2025-01-08 06:20:43 +00:00
var sum int
for y := range graph.data {
for x := range graph.data[y] {
2025-01-08 06:20:43 +00:00
current := newPoint(x, y)
if graph.valueAt(current) == 'X' {
for _, n := range neighbours(current) {
if checkNeighbours(n, "MAS", graph) {
sum++
2024-12-05 01:31:44 +00:00
}
}
2025-01-08 06:20:43 +00:00
}
2024-12-05 01:31:44 +00:00
}
}
return sum, nil
}
func checkNeighbours(n point, word string, g *graph) bool {
2024-12-05 01:31:44 +00:00
if len(word) == 0 {
log.Debug("we found a full XMAS")
return true
}
if g.isOutOfBounds(n) {
2024-12-05 01:31:44 +00:00
return false
}
if g.valueAt(n) != rune(word[0]) {
2024-12-05 01:31:44 +00:00
return false
}
return checkNeighbours(neighbours(n)[n.direction], word[1:], g)
2024-12-05 01:31:44 +00:00
}