58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package day23
|
|
|
|
import (
|
|
mapset "github.com/deckarep/golang-set/v2"
|
|
)
|
|
|
|
type PathEnd struct {
|
|
end Coord
|
|
visited mapset.Set[Coord]
|
|
}
|
|
|
|
func ExtendPath(p PathEnd, f Field) (nextPaths []PathEnd) {
|
|
endPointNeighbors := f.Neighbors(p.end)
|
|
for _, potentialNewEnd := range endPointNeighbors {
|
|
if !p.visited.Contains(potentialNewEnd) {
|
|
nextVisited := p.visited.Clone()
|
|
nextVisited.Add(p.end)
|
|
nextPaths = append(nextPaths, PathEnd{
|
|
end: potentialNewEnd,
|
|
visited: nextVisited,
|
|
})
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// return paths that end on End
|
|
func RunAllScenicPaths(f Field) (result []PathEnd) {
|
|
pathsToFurther := []PathEnd{
|
|
{end: Coord{Row: 0, Col: f.StartCol}, visited: mapset.NewSet[Coord]()},
|
|
}
|
|
|
|
theEndCoord := Coord{Row: f.MaxRow, Col: f.EndCol}
|
|
|
|
for len(pathsToFurther) > 0 {
|
|
curCheckedPath := pathsToFurther[0]
|
|
pathsToFurther = pathsToFurther[1:]
|
|
|
|
if curCheckedPath.end == theEndCoord {
|
|
result = append(result, curCheckedPath)
|
|
continue
|
|
}
|
|
|
|
nextSteps := ExtendPath(curCheckedPath, f)
|
|
|
|
// log.Printf("for %+v next steps %+v\n", curCheckedPath, pathsToFurther)
|
|
// log.Printf("remaining paths to check len is %d", len(pathsToFurther))
|
|
// log.Println(pathsToFurther)
|
|
|
|
if len(nextSteps) > 0 {
|
|
pathsToFurther = append(pathsToFurther, nextSteps...)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|