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