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

View File

@ -3,7 +3,6 @@ package two
import (
"bytes"
"slices"
"sync"
)
func Solve(buf []byte) (int, error) {
@ -13,56 +12,39 @@ func Solve(buf []byte) (int, error) {
return 0, err
}
wg := sync.WaitGroup{}
sumChan := make(chan bool)
var sum int
for y := range graph.data {
for x := range graph.data[y] {
wg.Add(1)
go func() {
defer wg.Done()
current := newPoint(x, y)
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
}
current := newPoint(x, y)
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()
return false
}() {
continue
}
}()
}
}
go func() {
wg.Wait()
close(sumChan)
}()
ns := neighbours(current)
matrix := newMatrix(
graph.valueAt(current),
graph.valueAt(ns[NW]),
graph.valueAt(ns[NE]),
graph.valueAt(ns[SE]),
graph.valueAt(ns[SW]),
)
var sum int
for val := range sumChan {
if val {
sum++
if matrix.isValid() {
sum++
}
}
}
}