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,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
}