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