Skip to content

Commit

Permalink
try to optimize BFS solution
Browse files Browse the repository at this point in the history
  • Loading branch information
byorgey committed Dec 5, 2023
1 parent 9a41a18 commit d564716
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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);
}

0 comments on commit d564716

Please sign in to comment.