mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
53 lines
869 B
Go
53 lines
869 B
Go
package two
|
|
|
|
import (
|
|
"bytes"
|
|
"slices"
|
|
)
|
|
|
|
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) == 'A' {
|
|
if func() bool {
|
|
for _, n := range neighbours(current) {
|
|
if graph.isOutOfBounds(n) {
|
|
return true
|
|
}
|
|
|
|
if !slices.Contains([]rune{'M', 'S'}, graph.valueAt(n)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}() {
|
|
continue
|
|
}
|
|
|
|
ns := neighbours(current)
|
|
matrix := newMatrix(
|
|
graph.valueAt(current),
|
|
graph.valueAt(ns[NW]),
|
|
graph.valueAt(ns[NE]),
|
|
graph.valueAt(ns[SE]),
|
|
graph.valueAt(ns[SW]),
|
|
)
|
|
|
|
if matrix.isValid() {
|
|
sum++
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return sum, nil
|
|
}
|