mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 14:50:46 +00:00
56 lines
977 B
Go
56 lines
977 B
Go
|
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
|
||
|
}
|