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