speed up p2...

This commit is contained in:
onyx-and-iris 2023-12-17 15:49:04 +00:00
parent 6dae7502db
commit 801ed17bf1

View File

@ -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,13 +20,18 @@ 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]) {
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
@ -33,11 +40,14 @@ func two(lines []string) int {
if n > res {
res = n
}
x++
}(i)
}
var y = 0
for y < len(lines) {
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
@ -47,7 +57,10 @@ func two(lines []string) int {
res = n
}
y++
}(i)
}
wg.Wait()
return res
}