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

49 lines
808 B
Go
Raw Permalink Normal View History

2024-12-13 10:02:48 +00:00
package two
import (
"slices"
"strings"
)
type point struct {
x int
y int
}
func newPoint(x, y int) point {
return point{x, y}
}
type graph struct {
data []string
}
func newGraph() *graph {
return &graph{}
}
func (g *graph) String() string {
return strings.Join(g.data, "\n")
}
func (g *graph) isOutOfBounds(p point) 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 point) rune {
return rune(g.data[p.y][p.x])
}
func (g *graph) sameKind(p, q point) bool {
return g.valueAt(p) == g.valueAt(q)
}
func (g *graph) debug(visited map[point]struct{}) *graph {
temp := newGraph()
temp.data = slices.Clone(g.data)
for point := range visited {
temp.data[point.y] = replaceAtIndex(temp.data[point.y], -1, point.x)
}
return temp
}