2024-12-05 01:31:44 +00:00
|
|
|
package one
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2025-01-07 17:22:03 +00:00
|
|
|
"sync"
|
2024-12-05 01:31:44 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2025-01-08 05:42:35 +00:00
|
|
|
for y := range graph.data {
|
|
|
|
for x := range graph.data[y] {
|
2025-01-07 17:22:03 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2025-01-08 05:42:35 +00:00
|
|
|
current := newPoint(x, y)
|
2025-01-07 17:22:03 +00:00
|
|
|
if graph.valueAt(current) == 'X' {
|
|
|
|
for _, n := range neighbours(current) {
|
|
|
|
sumChan <- checkNeighbours(n, "MAS", graph)
|
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
|
|
|
|
}
|
|
|
|
|
2025-01-07 17:22:03 +00:00
|
|
|
func checkNeighbours(n point, word string, g *graph) bool {
|
2024-12-05 01:31:44 +00:00
|
|
|
if len(word) == 0 {
|
|
|
|
log.Debug("we found a full XMAS")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2025-01-07 17:22:03 +00:00
|
|
|
if g.isOutOfBounds(n) {
|
2024-12-05 01:31:44 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2025-01-07 17:22:03 +00:00
|
|
|
if g.valueAt(n) != rune(word[0]) {
|
2024-12-05 01:31:44 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2025-01-07 17:22:03 +00:00
|
|
|
return checkNeighbours(neighbours(n)[n.direction], word[1:], g)
|
2024-12-05 01:31:44 +00:00
|
|
|
}
|