package two import ( "bytes" "slices" "sync" ) func Solve(buf []byte) (int, error) { r := bytes.NewReader(buf) graph, err := readLines(r) if err != nil { return 0, err } wg := sync.WaitGroup{} sumChan := make(chan bool) for i := 0; i < len(graph.data); i++ { for j := 0; j < len(graph.data[i]); j++ { wg.Add(1) go func() { defer wg.Done() current := newPoint(j, i) 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 }() { return } ns := neighbours(current) matrix := newMatrix( graph.valueAt(current), graph.valueAt(ns[NW]), graph.valueAt(ns[NE]), graph.valueAt(ns[SE]), graph.valueAt(ns[SW]), ) sumChan <- matrix.isValid() } }() } } go func() { wg.Wait() close(sumChan) }() var sum int for val := range sumChan { if val { sum++ } } return sum, nil }