mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
42 lines
990 B
Go
42 lines
990 B
Go
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}
|
|
}
|