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

53 lines
869 B
Go
Raw Normal View History

2024-12-05 01:31:44 +00:00
package two
import (
"bytes"
"slices"
)
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) == 'A' {
if func() bool {
for _, n := range neighbours(current) {
if graph.isOutOfBounds(n) {
return true
}
2025-01-08 06:20:43 +00:00
if !slices.Contains([]rune{'M', 'S'}, graph.valueAt(n)) {
return true
2024-12-05 01:31:44 +00:00
}
}
2025-01-08 06:20:43 +00:00
return false
}() {
continue
2024-12-05 01:31:44 +00:00
}
2025-01-08 06:20:43 +00:00
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++
}
}
2024-12-05 01:31:44 +00:00
}
}
return sum, nil
}