day23, still bad
This commit is contained in:
parent
28cf35e0e4
commit
0c31596018
|
@ -8,14 +8,15 @@ import (
|
||||||
// length of longest scenic route
|
// length of longest scenic route
|
||||||
func Run() int {
|
func Run() int {
|
||||||
fmt.Println("day 23")
|
fmt.Println("day 23")
|
||||||
filename := "day23/example"
|
filename := "day23/input"
|
||||||
field := ReadField(filename)
|
field := ReadField(filename)
|
||||||
finalPaths := RunDFSTingy(field)
|
finalPaths := RunDFSTingy(field)
|
||||||
log.Println(finalPaths)
|
// log.Println(finalPaths)
|
||||||
|
|
||||||
max := 0
|
max := 0
|
||||||
for _, path := range finalPaths {
|
for _, path := range finalPaths {
|
||||||
if path.Visited.Cardinality() > max {
|
if path.Visited.Cardinality() > max {
|
||||||
|
log.Println("one path len is ", path.Visited.Cardinality())
|
||||||
max = path.Visited.Cardinality()
|
max = path.Visited.Cardinality()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
#.#####################
|
||||||
|
#.#####################
|
||||||
|
#.##............#######
|
||||||
|
#.##.##########.#######
|
||||||
|
#....##########.#######
|
||||||
|
####..#########.#######
|
||||||
|
#####...........#######
|
||||||
|
###############.#######
|
||||||
|
###############.#######
|
||||||
|
###############.#######
|
|
@ -70,3 +70,9 @@ so if i'm in visited, it doesn't mean that stored is shorter and current is a de
|
||||||
|
|
||||||
but dfs should mean that all paths from this prefix have finished.
|
but dfs should mean that all paths from this prefix have finished.
|
||||||
so, sure. there have to be all done?
|
so, sure. there have to be all done?
|
||||||
|
** my example2 has fork on row 3 col 10
|
||||||
|
so 4,10 and 3,11 should be visited separately.
|
||||||
|
|
||||||
|
6,17 is where they join and the point which should have second entry
|
||||||
|
** allright, ugh. my new solution is memory hogging.
|
||||||
|
maybe i can draw the stuff and it will be several neat thingies
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package day23
|
package day23
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
mapset "github.com/deckarep/golang-set/v2"
|
mapset "github.com/deckarep/golang-set/v2"
|
||||||
|
@ -10,6 +11,9 @@ type PathEnd struct {
|
||||||
end Coord
|
end Coord
|
||||||
visited mapset.Set[Coord]
|
visited mapset.Set[Coord]
|
||||||
}
|
}
|
||||||
|
func (p PathEnd)Sring() string {
|
||||||
|
return fmt.Sprintf("PathEnd[at %+v, visited: %+v]", p.end, p.visited)
|
||||||
|
}
|
||||||
|
|
||||||
func ExtendPath(p PathEnd, f Field) (nextPaths []PathEnd) {
|
func ExtendPath(p PathEnd, f Field) (nextPaths []PathEnd) {
|
||||||
endPointNeighbors := f.NeighborsPart2(p.end)
|
endPointNeighbors := f.NeighborsPart2(p.end)
|
||||||
|
@ -41,13 +45,27 @@ func RunDFSTingy(f Field) []PathInfo {
|
||||||
return DFSScenicPaths(f, initialPath, initialShared)
|
return DFSScenicPaths(f, initialPath, initialShared)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var knownMax int = 0
|
||||||
|
func CheckAndPrintMax(maybeNewMax int) {
|
||||||
|
if maybeNewMax > knownMax {
|
||||||
|
log.Printf("\n\n>>>>found new max: %d\n", maybeNewMax)
|
||||||
|
knownMax = maybeNewMax
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func DFSScenicPaths(f Field, curPath PathEnd,
|
func DFSScenicPaths(f Field, curPath PathEnd,
|
||||||
sharedMem map[Coord][]PathInfo) (pathsFromTheStartToEnd []PathInfo) {
|
sharedMem map[Coord][]PathInfo) (pathsFromTheStartToEnd []PathInfo) {
|
||||||
|
|
||||||
curCoord := curPath.end
|
curCoord := curPath.end
|
||||||
|
|
||||||
|
if curCoord == (Coord{ Row: 6, Col: 15 }) {
|
||||||
|
log.Println(">>>>>>>>")
|
||||||
|
}
|
||||||
|
// log.Printf("entering %+v with mem %+v\n", curPath, sharedMem[curCoord])
|
||||||
|
|
||||||
if curCoord == f.EndCoord() {
|
if curCoord == f.EndCoord() {
|
||||||
pathsFromTheStartToEnd = append(pathsFromTheStartToEnd, PathInfo{curPath.visited.Clone()})
|
pathsFromTheStartToEnd = append(pathsFromTheStartToEnd, PathInfo{curPath.visited.Clone()})
|
||||||
log.Printf("got to end. visited %+v . will return %+v\n", curPath.visited, pathsFromTheStartToEnd)
|
log.Printf("got to end. cur len is %d\n", curPath.visited.Cardinality())
|
||||||
|
CheckAndPrintMax(curPath.visited.Cardinality())
|
||||||
// i guess return only from current to end?
|
// i guess return only from current to end?
|
||||||
// and on non terminal first time, return copy with self added?
|
// and on non terminal first time, return copy with self added?
|
||||||
return
|
return
|
||||||
|
@ -70,13 +88,18 @@ func DFSScenicPaths(f Field, curPath PathEnd,
|
||||||
suffix := PathInfo{
|
suffix := PathInfo{
|
||||||
Visited: path.Visited.Difference(curPath.visited).Clone(),
|
Visited: path.Visited.Difference(curPath.visited).Clone(),
|
||||||
}
|
}
|
||||||
|
// log.Printf(">> from path \n%+v make suffix \n%+v\n\n", path, suffix)
|
||||||
suffixesFromCurToEnd = append(suffixesFromCurToEnd, suffix)
|
suffixesFromCurToEnd = append(suffixesFromCurToEnd, suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
pathsFromTheStartToEnd = append(pathsFromTheStartToEnd, pathsToEndThrough...)
|
pathsFromTheStartToEnd = append(pathsFromTheStartToEnd, pathsToEndThrough...)
|
||||||
|
|
||||||
|
if len(pathsToEndThrough) != 0 {
|
||||||
|
// log.Printf("setting mem for %+v to %+v", curCoord, suffixesFromCurToEnd)
|
||||||
|
sharedMem[curCoord] = suffixesFromCurToEnd
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sharedMem[curCoord] = suffixesFromCurToEnd
|
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
// have visited this point before, due to dfs all possible paths to end should already be known
|
// have visited this point before, due to dfs all possible paths to end should already be known
|
||||||
|
@ -95,8 +118,11 @@ func DFSScenicPaths(f Field, curPath PathEnd,
|
||||||
fromCurPrefixToEnd := thisPrefix.Clone()
|
fromCurPrefixToEnd := thisPrefix.Clone()
|
||||||
fromCurPrefixToEnd.Union(fromCurToEnd)
|
fromCurPrefixToEnd.Union(fromCurToEnd)
|
||||||
pathsFromTheStartToEnd = append(pathsFromTheStartToEnd, PathInfo{fromCurPrefixToEnd})
|
pathsFromTheStartToEnd = append(pathsFromTheStartToEnd, PathInfo{fromCurPrefixToEnd})
|
||||||
|
log.Printf("additional path to end of len %d\n", fromCurPrefixToEnd.Cardinality())
|
||||||
|
CheckAndPrintMax(fromCurPrefixToEnd.Cardinality())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Printf("having second visit into %+v.\n", curPath)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -116,7 +142,7 @@ func RunAllScenicPaths(f Field) (result []PathEnd) {
|
||||||
|
|
||||||
if curCheckedPath.end == f.EndCoord() {
|
if curCheckedPath.end == f.EndCoord() {
|
||||||
result = append(result, curCheckedPath)
|
result = append(result, curCheckedPath)
|
||||||
log.Printf("found end path of len %d . %+v", curCheckedPath.visited.Cardinality(), curCheckedPath)
|
// log.Printf("found end path of len %d . %+v", curCheckedPath.visited.Cardinality(), curCheckedPath)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue