Solve AOC day 12 pt 2
This commit is contained in:
@@ -69,7 +69,9 @@
|
|||||||
(day 12
|
(day 12
|
||||||
(load "day12.kiss")
|
(load "day12.kiss")
|
||||||
(assert (= 10 .length (allPaths "start" "end" "src/year2021/inputs/day12-example.txt")))
|
(assert (= 10 .length (allPaths "start" "end" "src/year2021/inputs/day12-example.txt")))
|
||||||
(assert (= 3421 .length (allPaths "start" "end" "src/year2021/inputs/day12.txt"))))
|
(assert (= 36 .length (allPaths "start" "end" "src/year2021/inputs/day12-example.txt" true)))
|
||||||
|
(assert (= 3421 .length (allPaths "start" "end" "src/year2021/inputs/day12.txt")))
|
||||||
|
(assert (= 84870 .length (allPaths "start" "end" "src/year2021/inputs/day12.txt" true))))
|
||||||
(dayTodo 13)
|
(dayTodo 13)
|
||||||
(dayTodo 14)
|
(dayTodo 14)
|
||||||
(dayTodo 15)
|
(dayTodo 15)
|
||||||
|
@@ -11,27 +11,38 @@
|
|||||||
(link to from m)))
|
(link to from m)))
|
||||||
m))
|
m))
|
||||||
|
|
||||||
(function :Array<kiss.List<String>> allPaths [start end file]
|
(function :Array<kiss.List<String>> allPaths [start end file &opt part2]
|
||||||
(let [pMap (pathsMap file)
|
(let [pMap (pathsMap file)
|
||||||
:Array<kiss.List<String>> paths []
|
&mut :Array<kiss.List<String>> paths [[start]]
|
||||||
:Array<Map<String,Bool>> pathsVisited []]
|
&mut :Array<Map<String,Bool>> pathsVisited []
|
||||||
(paths.push [start])
|
&mut :Array<Bool> pathsDoubleVisited [false]
|
||||||
|
:Array<kiss.List<String>> finishedPaths []]
|
||||||
(let [pathVisited (new Map)]
|
(let [pathVisited (new Map)]
|
||||||
(dictSet pathVisited start true)
|
(dictSet pathVisited start true)
|
||||||
(pathsVisited.push pathVisited))
|
(pathsVisited.push pathVisited))
|
||||||
(until (apply = (concat [end] (for path paths (last path))))
|
(while paths
|
||||||
(doFor idx (reversed (collect (range paths.length)))
|
(let [:Array<kiss.List<String>> nextPaths []
|
||||||
(let [path (nth paths idx)
|
:Array<Map<String,Bool>> nextPathsVisited []
|
||||||
pathVisited (nth pathsVisited idx)
|
:kiss.List<Bool> nextPathsDoubleVisited []]
|
||||||
position (last path)]
|
(while paths
|
||||||
(when (= end position) (continue))
|
(let [path (paths.pop)
|
||||||
(paths.splice idx 1)
|
pathVisited (pathsVisited.pop)
|
||||||
(pathsVisited.splice idx 1)
|
pathDoubleVisited (pathsDoubleVisited.pop)
|
||||||
(doFor nextPosition (dictGet pMap position)
|
position (last path)]
|
||||||
(unless (dictGet pathVisited nextPosition)
|
(when (= end position)
|
||||||
(paths.insert idx (concat (path.copy) [nextPosition]))
|
(finishedPaths.push path)
|
||||||
(let [nextPathVisited (pathVisited.copy)]
|
(continue))
|
||||||
(when (nextPosition.isLowerCase)
|
(doFor nextPosition (dictGet pMap position)
|
||||||
(dictSet nextPathVisited nextPosition true))
|
(let [alreadyVisited (dictGet pathVisited nextPosition)]
|
||||||
(pathsVisited.insert idx nextPathVisited)))))))
|
(when (or !alreadyVisited (and part2 !pathDoubleVisited !(= nextPosition start)))
|
||||||
paths))
|
(nextPathsDoubleVisited.push pathDoubleVisited)
|
||||||
|
(when alreadyVisited (setNth nextPathsDoubleVisited -1 alreadyVisited))
|
||||||
|
(nextPaths.push (concat (path.copy) [nextPosition]))
|
||||||
|
(let [nextPathVisited (pathVisited.copy)]
|
||||||
|
(when (nextPosition.isLowerCase)
|
||||||
|
(dictSet nextPathVisited nextPosition true))
|
||||||
|
(nextPathsVisited.push nextPathVisited)))))))
|
||||||
|
(set paths nextPaths)
|
||||||
|
(set pathsVisited nextPathsVisited)
|
||||||
|
(set pathsDoubleVisited nextPathsDoubleVisited)))
|
||||||
|
finishedPaths))
|
Reference in New Issue
Block a user