remove the goroutines + waitgroups.

This commit is contained in:
onyx-and-iris 2025-01-08 06:20:43 +00:00
parent 5d6d8fdf79
commit a93e6a7a05
2 changed files with 33 additions and 69 deletions

View File

@ -2,7 +2,6 @@ package one
import ( import (
"bytes" "bytes"
"sync"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -14,36 +13,19 @@ func Solve(buf []byte) (int, error) {
return 0, err return 0, err
} }
wg := sync.WaitGroup{} var sum int
sumChan := make(chan bool)
for y := range graph.data { for y := range graph.data {
for x := range graph.data[y] { for x := range graph.data[y] {
wg.Add(1)
go func() {
defer wg.Done()
current := newPoint(x, y) current := newPoint(x, y)
if graph.valueAt(current) == 'X' { if graph.valueAt(current) == 'X' {
for _, n := range neighbours(current) { for _, n := range neighbours(current) {
sumChan <- checkNeighbours(n, "MAS", graph) if checkNeighbours(n, "MAS", graph) {
}
}
}()
}
}
go func() {
wg.Wait()
close(sumChan)
}()
var sum int
for val := range sumChan {
if val {
sum++ sum++
} }
} }
}
}
}
return sum, nil return sum, nil
} }

View File

@ -3,7 +3,6 @@ package two
import ( import (
"bytes" "bytes"
"slices" "slices"
"sync"
) )
func Solve(buf []byte) (int, error) { func Solve(buf []byte) (int, error) {
@ -13,15 +12,9 @@ func Solve(buf []byte) (int, error) {
return 0, err return 0, err
} }
wg := sync.WaitGroup{} var sum int
sumChan := make(chan bool)
for y := range graph.data { for y := range graph.data {
for x := range graph.data[y] { for x := range graph.data[y] {
wg.Add(1)
go func() {
defer wg.Done()
current := newPoint(x, y) current := newPoint(x, y)
if graph.valueAt(current) == 'A' { if graph.valueAt(current) == 'A' {
if func() bool { if func() bool {
@ -36,7 +29,7 @@ func Solve(buf []byte) (int, error) {
} }
return false return false
}() { }() {
return continue
} }
ns := neighbours(current) ns := neighbours(current)
@ -48,23 +41,12 @@ func Solve(buf []byte) (int, error) {
graph.valueAt(ns[SW]), graph.valueAt(ns[SW]),
) )
sumChan <- matrix.isValid() if matrix.isValid() {
}
}()
}
}
go func() {
wg.Wait()
close(sumChan)
}()
var sum int
for val := range sumChan {
if val {
sum++ sum++
} }
} }
}
}
return sum, nil return sum, nil
} }