aoc2024/day-14/internal/two/solve.go

52 lines
891 B
Go
Raw Permalink Normal View History

2024-12-14 19:11:58 +00:00
package two
import (
"bytes"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
robots, err := parseLines(r)
if err != nil {
return 0, err
}
const maxSeconds int = 10e3
const width, height dimension = 101, 103
var graph *graph
var max, maxAtSecond int
for i := 1; i <= maxSeconds; i++ {
graph = newGraph(width, height)
for _, robot := range robots {
robot.update(width, height)
}
graph.update(robots)
2025-01-07 17:25:05 +00:00
numNeighbours := evaluateNeighbours(robots, graph)
2024-12-14 19:11:58 +00:00
if numNeighbours > max {
max = numNeighbours
maxAtSecond = i
}
}
return maxAtSecond, nil
}
2025-01-07 17:25:05 +00:00
func evaluateNeighbours(robots []*robot, g *graph) int {
2024-12-14 19:11:58 +00:00
var numNeighbours int
for _, robot := range robots {
for _, n := range neighbours(robot.position) {
2025-01-07 17:25:05 +00:00
if g.isOutOfBounds(n) {
2024-12-14 19:11:58 +00:00
continue
}
2025-01-07 17:25:05 +00:00
if g.valueAt(n) > 0 {
2024-12-14 19:11:58 +00:00
numNeighbours++
}
}
}
return numNeighbours
}