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

42 lines
990 B
Go
Raw Normal View History

2024-12-14 19:11:58 +00:00
package one
import (
"bytes"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
robots, err := parseLines(r)
if err != nil {
return 0, err
}
const numSeconds int = 100
const width, height dimension = 101, 103
graph := newGraph(width, height)
for range numSeconds {
for _, robot := range robots {
robot.update(width, height)
}
}
graph.update(robots)
safetyFactor := 1
for _, v := range calculateQuadrants(graph) {
safetyFactor *= v
}
return safetyFactor, nil
}
func calculateQuadrants(graph *graph) []int {
topLeft := graph.quadrant(0, len(graph.data[0])/2, 0, len(graph.data)/2)
topRight := graph.quadrant(len(graph.data[0])/2+1, len(graph.data[0]), 0, len(graph.data)/2)
bottomLeft := graph.quadrant(0, len(graph.data[0])/2, len(graph.data)/2+1, len(graph.data))
bottomRight := graph.quadrant(len(graph.data[0])/2+1, len(graph.data[0]), len(graph.data)/2+1, len(graph.data))
return []int{topLeft, topRight, bottomLeft, bottomRight}
}