Skip to content

Commit

Permalink
palanquin scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Aug 6, 2024
1 parent 52d5e2c commit 0f45dc6
Show file tree
Hide file tree
Showing 3 changed files with 402 additions and 0 deletions.
78 changes: 78 additions & 0 deletions data/scenarios/Challenges/_palanquin/emperor.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
def goDir = \f. \r.
let d = fst r in
if (d == down) {
grapesHere <- ishere "grapes";
if grapesHere {
grab; return ()
} {};
return ();
} {
turn d;

// An obstruction might arise after
// navigation direction is determined
// but before we move.
try {
move;
} {};
f;
}
end;

def followRoute = \item.
nextDir <- path (inL ()) (inR item);
case nextDir return $ goDir $ followRoute item;
end;

def getGrapes =
let targetItem = "grapes" in
emperorHasThem <- has targetItem;
if emperorHasThem {
say "Tally ho!";
} {
grapesDropped <- as base {
baseHasThem <- has targetItem;
return $ not baseHasThem;
};

if grapesDropped {
followRoute targetItem;
} {
wait 10;
getGrapes;
};
}
end;

def avoidSides =

toLeft <- scan back;
case toLeft return (\_.
turn right;
);

toRight <- scan back;
case toRight return (\_.
turn left;
);

behind <- scan back;
case behind return (\_.
isBlocked <- blocked;
if isBlocked {} {move;}
);

watch forward;
watch back;
watch left;
watch right;
wait 1000;
end;

def go =
getGrapes;
avoidSides;
go;
end;

go;
118 changes: 118 additions & 0 deletions data/scenarios/Challenges/_palanquin/solution.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
def doN = \n. \f. if (n > 0) {f; doN (n - 1) f} {}; end;

def intersperse = \n. \f2. \f1. if (n > 0) {
f1;
if (n > 1) {
f2;
} {};
intersperse (n - 1) f2 f1;
} {};
end;

def shiftRightForPush =
turn back;
move;
turn left;
move;
turn left;
end;

/*
Precondition:
Positioned behind the wall, facing it, on the leftmost cell.
*/
def pushWall =
intersperse 3 shiftRightForPush push;
end;

def moveToBackWall =
turn back;
doN 5 move;
turn right;
doN 2 move;
turn right;
end;

def moveRightSide =
turn right;
push;
turn right;
move;
turn left;
move;
turn left;
doN 5 push;
turn right;
move;
turn left;
move;
turn left;
push;
end;

def moveLeftSide =
doN 4 move;
turn left;
doN 5 move;
turn right;
push;
turn left;
move;
turn right;
move;
turn right;
doN 5 push;
turn left;
move;
turn right;
move;
turn right;
push;
end;

def initialSetup =
turn south;
doN 3 move;
turn right;
move;
turn right;
end;

def waitUntilGrapesGrabbed =
itemHere <- scan forward;
case itemHere return (\_.
watch forward;
wait 1000;
waitUntilGrapesGrabbed;
);
end;

def placeGrapes =

doN 4 move;
place "grapes";
turn back;
move;
doN 2 push;
turn back;
doN 2 move;
waitUntilGrapesGrabbed;

turn back;
doN 4 move;
turn back;
doN 2 push;
end;

def go =
placeGrapes;

// initialSetup;
// pushWall;
// moveToBackWall;
// pushWall;
// moveRightSide;
// moveLeftSide;
end;

go;
Loading

0 comments on commit 0f45dc6

Please sign in to comment.