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

56 lines
977 B
Go
Raw Normal View History

2024-12-14 19:11:58 +00:00
package two
import (
"bytes"
log "github.com/sirupsen/logrus"
)
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)
numNeighbours := evaluateNeighbours(graph, robots)
if numNeighbours > max {
max = numNeighbours
maxAtSecond = i
}
}
log.Debugf("\n%s\n", graph.String())
return maxAtSecond, nil
}
func evaluateNeighbours(graph *graph, robots []*robot) int {
var numNeighbours int
for _, robot := range robots {
for _, n := range neighbours(robot.position) {
if graph.isOutOfBounds(n) {
continue
}
if graph.valueAt(n) > 0 {
numNeighbours++
}
}
}
return numNeighbours
}