From a93e6a7a05dda5ab44da708f1751824ae273aeda Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Wed, 8 Jan 2025 06:20:43 +0000 Subject: [PATCH] remove the goroutines + waitgroups. --- day-04/internal/one/solve.go | 32 ++++------------- day-04/internal/two/solve.go | 70 ++++++++++++++---------------------- 2 files changed, 33 insertions(+), 69 deletions(-) diff --git a/day-04/internal/one/solve.go b/day-04/internal/one/solve.go index b99f7fb..0158f58 100644 --- a/day-04/internal/one/solve.go +++ b/day-04/internal/one/solve.go @@ -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++ + } } } diff --git a/day-04/internal/two/solve.go b/day-04/internal/two/solve.go index 548d11c..be1531d 100644 --- a/day-04/internal/two/solve.go +++ b/day-04/internal/two/solve.go @@ -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++ + } + } } }