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

42 lines
922 B
Go
Raw Permalink 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
}
2025-01-07 17:25:05 +00:00
func calculateQuadrants(g *graph) []int {
topLeft := g.quadrant(0, len(g.data[0])/2, 0, len(g.data)/2)
topRight := g.quadrant(len(g.data[0])/2+1, len(g.data[0]), 0, len(g.data)/2)
bottomLeft := g.quadrant(0, len(g.data[0])/2, len(g.data)/2+1, len(g.data))
bottomRight := g.quadrant(len(g.data[0])/2+1, len(g.data[0]), len(g.data)/2+1, len(g.data))
2024-12-14 19:11:58 +00:00
return []int{topLeft, topRight, bottomLeft, bottomRight}
}