diff --git a/day-16/two.go b/day-16/two.go index 72978c5..0fa59ec 100644 --- a/day-16/two.go +++ b/day-16/two.go @@ -1,5 +1,7 @@ package main +import "sync" + // returns the number of unique nodes (reducing multiple nodes with different directions to one) func uniqueNodes(move *mover) int { uniqueCoords := []coords{} @@ -18,36 +20,47 @@ func spawn(i, j, direction int, lines []string) int { return uniqueNodes(m) } +var wg sync.WaitGroup + // two returns the highest energized value for any beam spawn point/direction func two(lines []string) int { res := 0 n := 0 - var x = 0 - for x < len(lines[0]) { - n = spawn(x, 0, S, lines) - if n > res { - res = n - } - n = spawn(x, len(lines[0])-1, N, lines) - if n > res { - res = n - } - x++ + for i := 0; i < len(lines[0]); i++ { + wg.Add(1) + go func(x int) { + defer wg.Done() + + n = spawn(x, 0, S, lines) + if n > res { + res = n + } + n = spawn(x, len(lines[0])-1, N, lines) + if n > res { + res = n + } + }(i) } - var y = 0 - for y < len(lines) { - n = spawn(0, y, E, lines) - if n > res { - res = n - } - n = spawn(len(lines[0])-1, y, W, lines) - if n > res { - res = n - } - y++ + for i := 0; i < len(lines); i++ { + wg.Add(1) + go func(y int) { + defer wg.Done() + + n = spawn(0, y, E, lines) + if n > res { + res = n + } + n = spawn(len(lines[0])-1, y, W, lines) + if n > res { + res = n + } + y++ + }(i) } + wg.Wait() + return res }