aoc2023/day-16/two.go
2023-12-17 15:31:11 +00:00

54 lines
1.0 KiB
Go

package main
// returns the number of unique nodes (reducing multiple nodes with different directions to one)
func uniqueNodes(move *mover) int {
uniqueCoords := []coords{}
for _, node := range move.nodes {
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)
}
// 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++
}
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++
}
return res
}