From d564716ebdd8c3228f87f01e6ef754fb509ef08d Mon Sep 17 00:00:00 2001 From: Brent Yorgey Date: Tue, 5 Dec 2023 05:51:37 -0600 Subject: [PATCH] try to optimize BFS solution --- .../Mazes/_shortest_path/shortest_path_sol.sw | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/data/scenarios/Challenges/Mazes/_shortest_path/shortest_path_sol.sw b/data/scenarios/Challenges/Mazes/_shortest_path/shortest_path_sol.sw index b28b11127..b0a2ffa6a 100644 --- a/data/scenarios/Challenges/Mazes/_shortest_path/shortest_path_sol.sw +++ b/data/scenarios/Challenges/Mazes/_shortest_path/shortest_path_sol.sw @@ -2,22 +2,25 @@ def tL = turn left end; def tR = turn right end; def tB = turn back end; def ifM = \p.\t.\e. b <- p; if b t e end; -def DFSn = \n. +// Returns true if blocked, false if it may be a viable path +def DFSn : int -> cmd bool = \n. // say $ "DFSn at level " ++ format n; ifM (ishere "goal") {swap "path"; selfdestruct} {}; if (n == 0) {} { ifM (ishere "path") {} { place "path"; - tL; b <- blocked; if b {} {move; DFSn (n-1)}; - tR; b <- blocked; if b {} {move; DFSn (n-1)}; - tR; b <- blocked; if b {} {move; DFSn (n-1)}; - tL; grab; return () + tL; b <- blocked; bL <- if b {return true} {move; DFSn (n-1)}; + tR; b <- blocked; bF <- if b {return true} {move; DFSn (n-1)}; + tR; b <- blocked; bR <- if b {return true} {move; DFSn (n-1)}; + tL; if (bL && bF && bR) {swap "rock"} {grab}; return () } }; - tB; move; tB + rockhere <- ishere "rock"; + tB; move; tB; + return rockhere end; def startDFS = \n. - b <- blocked; if b {} {move; DFSn n} + b <- blocked; if b {} {move; DFSn n; return ()} end; def clear_rocks = ifM (ishere "rock") { @@ -42,5 +45,6 @@ def for : int -> int -> (int -> cmd unit) -> cmd unit = \lo. \hi. \m. end; build { require 500 "rock"; require 500 "path"; + log "hi"; for 1 500 (\n. DFS n); }