aoc2024/day-18/internal/one/graph.go

51 lines
1022 B
Go
Raw Normal View History

2024-12-18 18:48:11 +00:00
package one
import (
"slices"
"strings"
)
type graph struct {
start Point
end Point
data []string
}
func newGraph(width, height, numCorruptions int, corruptedCoords [][]int) *graph {
var data []string
var sb strings.Builder
for range height {
for range width {
sb.WriteRune('.')
}
data = append(data, sb.String())
sb.Reset()
}
for _, coords := range corruptedCoords[:numCorruptions] {
data[coords[1]] = replaceAtIndex(data[coords[1]], '#', coords[0])
}
return &graph{Point{0, 0}, Point{len(data[0]) - 1, len(data) - 1}, data}
}
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) debug(path []Point) string {
temp := slices.Clone(g.data)
for _, p := range path {
temp[p.Y] = replaceAtIndex(temp[p.Y], 'O', p.X)
}
return strings.Join(temp, "\n")
}