mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 22:30:47 +00:00
49 lines
787 B
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)
|
|
}
|