aoc2024/day-14/internal/two/graph.go

60 lines
1.0 KiB
Go
Raw Normal View History

2024-12-14 19:11:58 +00:00
package two
import (
"strings"
)
type dimension int
type graph struct {
x dimension
y dimension
data [][]int
}
func newGraph(x, y dimension) *graph {
var data [][]int
for range y {
data = append(data, make([]int, x))
}
return &graph{x: x, y: y, data: data}
}
func (g *graph) String() string {
temp := []string{}
for range len(g.data) {
temp = append(temp, string(make([]byte, len(g.data[0]))))
}
for i := 0; i < len(g.data); i++ {
for j := 0; j < len(g.data[0]); j++ {
if g.data[i][j] == 0 {
temp[i] = replaceAtIndex(temp[i], '.', j)
} else {
temp[i] = replaceAtIndex(temp[i], '*', j)
}
}
}
return strings.Join(temp, "\n")
}
func (g *graph) update(robots []*robot) {
for _, robot := range robots {
g.updateEach(robot.position)
}
}
func (g *graph) updateEach(p position) {
g.data[p.y][p.x]++
}
func (g *graph) isOutOfBounds(p position) bool {
return p.x < 0 || p.y < 0 || p.y >= len(g.data) || p.x >= len(g.data[p.y])
}
func (g *graph) valueAt(p position) int {
return g.data[p.y][p.x]
}