2024-12-05 01:31:44 +00:00
|
|
|
package two
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"slices"
|
2025-01-07 17:22:03 +00:00
|
|
|
"sync"
|
2024-12-05 01:31:44 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2025-01-07 17:22:03 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
sumChan := make(chan bool)
|
2024-12-05 01:31:44 +00:00
|
|
|
|
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++ {
|
2025-01-07 17:22:03 +00:00
|
|
|
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
|
|
|
|
}
|
2024-12-26 20:09:19 +00:00
|
|
|
|
2025-01-07 17:22:03 +00:00
|
|
|
if !slices.Contains([]rune{'M', 'S'}, graph.valueAt(n)) {
|
|
|
|
return true
|
|
|
|
}
|
2024-12-05 01:31:44 +00:00
|
|
|
}
|
2025-01-07 17:22:03 +00:00
|
|
|
return false
|
|
|
|
}() {
|
|
|
|
return
|
2024-12-05 01:31:44 +00:00
|
|
|
}
|
|
|
|
|
2025-01-07 17:22:03 +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]),
|
|
|
|
)
|
|
|
|
|
|
|
|
sumChan <- matrix.isValid()
|
2024-12-05 01:31:44 +00:00
|
|
|
}
|
2025-01-07 17:22:03 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(sumChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
var sum int
|
|
|
|
for val := range sumChan {
|
|
|
|
if val {
|
|
|
|
sum++
|
2024-12-05 01:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum, nil
|
|
|
|
}
|