mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 22:30:47 +00:00
remove the goroutines + waitgroups.
This commit is contained in:
parent
5d6d8fdf79
commit
a93e6a7a05
@ -2,7 +2,6 @@ package one
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@ -14,36 +13,19 @@ 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)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(sumChan)
|
||||
}()
|
||||
|
||||
var sum int
|
||||
for val := range sumChan {
|
||||
if val {
|
||||
if checkNeighbours(n, "MAS", graph) {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package two
|
||||
import (
|
||||
"bytes"
|
||||
"slices"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
@ -13,15 +12,9 @@ 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 {
|
||||
@ -36,7 +29,7 @@ func Solve(buf []byte) (int, error) {
|
||||
}
|
||||
return false
|
||||
}() {
|
||||
return
|
||||
continue
|
||||
}
|
||||
|
||||
ns := neighbours(current)
|
||||
@ -48,23 +41,12 @@ func Solve(buf []byte) (int, error) {
|
||||
graph.valueAt(ns[SW]),
|
||||
)
|
||||
|
||||
sumChan <- matrix.isValid()
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(sumChan)
|
||||
}()
|
||||
|
||||
var sum int
|
||||
for val := range sumChan {
|
||||
if val {
|
||||
if matrix.isValid() {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user