aoc2023/day-16/two.go

78 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-12-17 15:31:11 +00:00
package main
2023-12-17 15:49:04 +00:00
import "sync"
2023-12-17 15:31:11 +00:00
// returns the number of unique nodes (reducing multiple nodes with different directions to one)
2023-12-22 22:23:43 +00:00
func uniqueNodes(mover *mover) int {
2023-12-17 15:31:11 +00:00
uniqueCoords := []coords{}
2023-12-22 22:23:43 +00:00
for _, node := range mover.nodes {
2023-12-17 15:31:11 +00:00
if !coordInCoords(node.coords, uniqueCoords) {
uniqueCoords = append(uniqueCoords, node.coords)
}
}
return len(uniqueCoords)
}
// spawn invoked a single runner with a single mover
func spawn(i, j, direction int, lines []string) int {
m := newMover(newNode(i, j, direction))
runner(m, lines)
return uniqueNodes(m)
}
2023-12-17 15:49:04 +00:00
var wg sync.WaitGroup
2023-12-17 15:31:11 +00:00
// two returns the highest energized value for any beam spawn point/direction
func two(lines []string) int {
res := 0
n := 0
2023-12-17 15:49:04 +00:00
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
}
2023-12-17 17:51:09 +00:00
}(i)
wg.Add(1)
go func(x int) {
defer wg.Done()
2023-12-17 15:49:04 +00:00
n = spawn(x, len(lines[0])-1, N, lines)
if n > res {
res = n
}
}(i)
2023-12-17 15:31:11 +00:00
}
2023-12-17 15:49:04 +00:00
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
}
2023-12-17 17:51:09 +00:00
}(i)
wg.Add(1)
go func(y int) {
defer wg.Done()
2023-12-17 15:49:04 +00:00
n = spawn(len(lines[0])-1, y, W, lines)
if n > res {
res = n
}
}(i)
2023-12-17 15:31:11 +00:00
}
2023-12-17 15:49:04 +00:00
wg.Wait()
2023-12-17 15:31:11 +00:00
return res
}