Skip to content

Commit

Permalink
Pied piper scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Oct 19, 2024
1 parent e99e5ca commit 2abee43
Show file tree
Hide file tree
Showing 3 changed files with 401 additions and 0 deletions.
116 changes: 116 additions & 0 deletions data/scenarios/Challenges/_pied-piper/rat.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
def checkExpireSelf =
loc <- whereami;
if (snd loc > 6) {selfdestruct} {};
end;


def moveWithMortality =
move;
instant checkExpireSelf;
end;

def goFoodDir = \f. \r.
// say "in goFoodDir";
let d = fst r in
if (d == down) {
foodHere <- ishere "oats";
if foodHere {
grab; return ()
} {};
f;
return ()
} {
turn d;

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

def goHomeDir = \f. \r.
// say "in goHomeDir";
let d = fst r in
if (d == down) {
return ()
} {
turn d;

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

def findGoodDirection =
// say "in findGoodDirection";
isBlocked <- blocked;
if isBlocked {
turn left;
findGoodDirection;
} {};
end;

def moveUntilBlocked =
isBlocked <- blocked;
if isBlocked {
} {
moveWithMortality;
moveUntilBlocked;
};
end;

def pauseAtRandom =
r <- random 3;
if (r == 0) {
r2 <- random 12;
wait $ 6 + r2;
} {}
end;

def returnHome = \homeLoc.
// say "in returnHome";
nextDir <- path (inL ()) (inL homeLoc);
case nextDir return $ goHomeDir $ returnHome homeLoc;
end;

def pursueFood = \hadSensedFood. \homeLoc.
// say $ "in pursueFood. hadSensedFood? " ++ format hadSensedFood;
nextDir <- path (inR 5) (inR "oats");
case nextDir (\_. if hadSensedFood {returnHome homeLoc} {return ()}) $
goFoodDir $ pursueFood true homeLoc;
end;


def doMovement = \startLoc.
// say "in doMovement";
checkExpireSelf;

findGoodDirection;
moveUntilBlocked;
pauseAtRandom;
pursueFood false startLoc;
end;

/** Loop */
def go = \startLoc.
doMovement startLoc;
go startLoc;
end;

startLoc <- whereami;
// let shouldDestruct = !(snd startLoc == -17 && fst startLoc < 19) in
let shouldDestruct = false in
if shouldDestruct {
selfdestruct;
} {
go startLoc;
}
39 changes: 39 additions & 0 deletions data/scenarios/Challenges/_pied-piper/solution.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def doN = \n. \f. if (n > 0) {f; doN (n - 1) f} {}; end;

def makeOatsCrumb = \spacing.
place "oats";
doN spacing move;
end;


def waitUntilDisappeared =
found <- scout north;
if found {
wait 1;
waitUntilDisappeared;
} {};
end;

/**
Place a trail of breadcrumbs, and return
one crumb short of the first crumb.
*/
def makeOatsTrail = \spacing. \segments.
doN segments $ makeOatsCrumb spacing;
turn back;
doN ((segments - 1)*spacing) move;
turn back;
watch down;
wait 2000;
end;

def go =
let spacing = 4 in
let segments = 5 in
makeOatsTrail spacing segments;
// Has probably awakened due to rat eating food here.
waitUntilDisappeared;
makeOatsTrail spacing $ segments;
end;

go;
Loading

0 comments on commit 2abee43

Please sign in to comment.