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)
|
2024-12-26 20:09:19 +00:00
|
|
|
graph, err := readLines(r)
|
2024-12-05 01:31:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var sum int
|
|
|
|
|
2024-12-26 20:09:19 +00:00
|
|
|
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
|
|
|
|
}
|
2024-12-05 01:31:44 +00:00
|
|
|
|
2024-12-26 20:09:19 +00:00
|
|
|
if checkNeighbours(graph, n, "MAS") {
|
2024-12-05 01:31:44 +00:00
|
|
|
sum++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum, nil
|
|
|
|
}
|
|
|
|
|
2024-12-26 20:09:19 +00:00
|
|
|
func checkNeighbours(graph *graph, n point, word string) bool {
|
2024-12-05 01:31:44 +00:00
|
|
|
if len(word) == 0 {
|
|
|
|
log.Debug("we found a full XMAS")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-12-26 20:09:19 +00:00
|
|
|
if graph.isOutOfBounds(n) {
|
2024-12-05 01:31:44 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-12-26 20:09:19 +00:00
|
|
|
if graph.valueAt(n) != rune(word[0]) {
|
2024-12-05 01:31:44 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-12-26 20:09:19 +00:00
|
|
|
return checkNeighbours(graph, neighbours(n)[n.direction], word[1:])
|
2024-12-05 01:31:44 +00:00
|
|
|
}
|