2024-12-18 18:48:11 +00:00
|
|
|
package one
|
|
|
|
|
|
|
|
import (
|
|
|
|
"slices"
|
|
|
|
"strings"
|
2024-12-19 21:32:21 +00:00
|
|
|
|
|
|
|
"github.com/onyx-and-iris/aoc2024/day-18/internal/point"
|
|
|
|
orderedmap "github.com/wk8/go-ordered-map/v2"
|
2024-12-18 18:48:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type graph struct {
|
2024-12-19 21:32:21 +00:00
|
|
|
start point.Point
|
|
|
|
end point.Point
|
2024-12-18 18:48:11 +00:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
|
2024-12-19 21:32:21 +00:00
|
|
|
return &graph{point.Point{X: 0, Y: 0}, point.Point{X: len(data[0]) - 1, Y: len(data) - 1}, data}
|
2024-12-18 18:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *graph) String() string {
|
|
|
|
return strings.Join(g.data, "\n")
|
|
|
|
}
|
|
|
|
|
2024-12-19 21:32:21 +00:00
|
|
|
func (g *graph) isOutOfBounds(p point.Point) bool {
|
2024-12-18 18:48:11 +00:00
|
|
|
return p.X < 0 || p.Y < 0 || p.Y >= len(g.data) || p.X >= len(g.data[p.Y])
|
|
|
|
}
|
|
|
|
|
2024-12-19 21:32:21 +00:00
|
|
|
func (g *graph) valueAt(p point.Point) rune {
|
2024-12-18 18:48:11 +00:00
|
|
|
return rune(g.data[p.Y][p.X])
|
|
|
|
}
|
|
|
|
|
2024-12-19 21:32:21 +00:00
|
|
|
func (g *graph) debug(path *orderedmap.OrderedMap[point.Point, struct{}]) string {
|
2024-12-18 18:48:11 +00:00
|
|
|
temp := slices.Clone(g.data)
|
2024-12-19 21:32:21 +00:00
|
|
|
temp[g.start.Y] = replaceAtIndex(temp[g.start.Y], 'O', g.start.X)
|
|
|
|
for pair := path.Oldest(); pair != nil; pair = pair.Next() {
|
|
|
|
temp[pair.Key.Y] = replaceAtIndex(temp[pair.Key.Y], 'O', pair.Key.X)
|
2024-12-18 18:48:11 +00:00
|
|
|
}
|
|
|
|
return strings.Join(temp, "\n")
|
|
|
|
}
|