package two import ( "bytes" "slices" "github.com/onyx-and-iris/aoc2024/day-04/internal/util" ) func Solve(data []byte) (int, error) { r := bytes.NewReader(data) lines, err := util.ReadLines(r) if err != nil { return 0, err } var sum int for i := 0; i < len(lines); i++ { for j := 0; j < len(lines[i]); j++ { if lines[i][j] == 'A' { neighbours := newNeighbours(j, i) if func() bool { for _, n := range neighbours.all() { if n.outOfBounds(lines) { return true } if !slices.Contains([]rune{'M', 'S'}, n.value(lines)) { return true } } return false }() { continue } matrix := newMatrix( rune(lines[i][j]), neighbours.NW.value(lines), neighbours.NE.value(lines), neighbours.SE.value(lines), neighbours.SW.value(lines), ) if matrix.validate() { sum++ } } } } return sum, nil }