mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 14:50:46 +00:00
52 lines
891 B
Go
52 lines
891 B
Go
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)
|
|
|
|
numNeighbours := evaluateNeighbours(robots, graph)
|
|
if numNeighbours > max {
|
|
max = numNeighbours
|
|
maxAtSecond = i
|
|
}
|
|
}
|
|
|
|
return maxAtSecond, nil
|
|
}
|
|
|
|
func evaluateNeighbours(robots []*robot, g *graph) int {
|
|
var numNeighbours int
|
|
for _, robot := range robots {
|
|
for _, n := range neighbours(robot.position) {
|
|
if g.isOutOfBounds(n) {
|
|
continue
|
|
}
|
|
|
|
if g.valueAt(n) > 0 {
|
|
numNeighbours++
|
|
}
|
|
}
|
|
}
|
|
return numNeighbours
|
|
}
|