day23, part1
This commit is contained in:
57
day23/paths.go
Normal file
57
day23/paths.go
Normal file
@@ -0,0 +1,57 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user