mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 22:30:47 +00:00
run loop logic in separate goroutines.
use bool channel + waitgroups to collect results
This commit is contained in:
parent
feb0ae4617
commit
c70d4a74cf
@ -2,6 +2,7 @@ package one
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"sync"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@ -13,37 +14,53 @@ func Solve(buf []byte) (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var sum int
|
wg := sync.WaitGroup{}
|
||||||
|
sumChan := make(chan bool)
|
||||||
|
|
||||||
for i := 0; i < len(graph.data); i++ {
|
for i := 0; i < len(graph.data); i++ {
|
||||||
for j := 0; j < len(graph.data[i]); j++ {
|
for j := 0; j < len(graph.data[i]); j++ {
|
||||||
current := newPoint(j, i)
|
wg.Add(1)
|
||||||
if graph.valueAt(current) == 'X' {
|
go func() {
|
||||||
for _, n := range neighbours(current) {
|
defer wg.Done()
|
||||||
if checkNeighbours(graph, n, "MAS") {
|
|
||||||
sum++
|
current := newPoint(j, i)
|
||||||
|
if graph.valueAt(current) == 'X' {
|
||||||
|
for _, n := range neighbours(current) {
|
||||||
|
sumChan <- checkNeighbours(n, "MAS", graph)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
wg.Wait()
|
||||||
|
close(sumChan)
|
||||||
|
}()
|
||||||
|
|
||||||
|
var sum int
|
||||||
|
for val := range sumChan {
|
||||||
|
if val {
|
||||||
|
sum++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sum, nil
|
return sum, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkNeighbours(graph *graph, n point, word string) bool {
|
func checkNeighbours(n point, word string, g *graph) bool {
|
||||||
if len(word) == 0 {
|
if len(word) == 0 {
|
||||||
log.Debug("we found a full XMAS")
|
log.Debug("we found a full XMAS")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if graph.isOutOfBounds(n) {
|
if g.isOutOfBounds(n) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if graph.valueAt(n) != rune(word[0]) {
|
if g.valueAt(n) != rune(word[0]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return checkNeighbours(graph, neighbours(n)[n.direction], word[1:])
|
return checkNeighbours(neighbours(n)[n.direction], word[1:], g)
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ func (m *matrix) rotate() *matrix {
|
|||||||
return &temp
|
return &temp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *matrix) validate() bool {
|
func (m *matrix) isValid() bool {
|
||||||
golden := &matrix{
|
golden := &matrix{
|
||||||
{'M', -1, 'M'},
|
{'M', -1, 'M'},
|
||||||
{-1, 'A', -1},
|
{-1, 'A', -1},
|
||||||
|
@ -3,6 +3,7 @@ package two
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"slices"
|
"slices"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Solve(buf []byte) (int, error) {
|
func Solve(buf []byte) (int, error) {
|
||||||
@ -12,40 +13,56 @@ func Solve(buf []byte) (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var sum int
|
wg := sync.WaitGroup{}
|
||||||
|
sumChan := make(chan bool)
|
||||||
|
|
||||||
for i := 0; i < len(graph.data); i++ {
|
for i := 0; i < len(graph.data); i++ {
|
||||||
for j := 0; j < len(graph.data[i]); j++ {
|
for j := 0; j < len(graph.data[i]); j++ {
|
||||||
current := newPoint(j, i)
|
wg.Add(1)
|
||||||
if graph.valueAt(current) == 'A' {
|
go func() {
|
||||||
if func() bool {
|
defer wg.Done()
|
||||||
for _, n := range neighbours(current) {
|
|
||||||
if graph.isOutOfBounds(n) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if !slices.Contains([]rune{'M', 'S'}, graph.valueAt(n)) {
|
current := newPoint(j, i)
|
||||||
return true
|
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
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
}() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
ns := neighbours(current)
|
ns := neighbours(current)
|
||||||
matrix := newMatrix(
|
matrix := newMatrix(
|
||||||
graph.valueAt(current),
|
graph.valueAt(current),
|
||||||
graph.valueAt(ns[NW]),
|
graph.valueAt(ns[NW]),
|
||||||
graph.valueAt(ns[NE]),
|
graph.valueAt(ns[NE]),
|
||||||
graph.valueAt(ns[SE]),
|
graph.valueAt(ns[SE]),
|
||||||
graph.valueAt(ns[SW]),
|
graph.valueAt(ns[SW]),
|
||||||
)
|
)
|
||||||
|
|
||||||
if matrix.validate() {
|
sumChan <- matrix.isValid()
|
||||||
sum++
|
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
wg.Wait()
|
||||||
|
close(sumChan)
|
||||||
|
}()
|
||||||
|
|
||||||
|
var sum int
|
||||||
|
for val := range sumChan {
|
||||||
|
if val {
|
||||||
|
sum++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user