Skip to content

Commit

Permalink
refine solution
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Oct 19, 2024
1 parent 31e4084 commit 7eec7ea
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 45 deletions.
31 changes: 9 additions & 22 deletions data/scenarios/Challenges/_pied-piper/rat.sw
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
// def checkExpireSelf =
// loc <- whereami;
// if (snd loc > 6) {selfdestruct} {};
// end;


def moveWithMortality =
move;
// instant checkExpireSelf;
end;

def goFoodDir = \f. \r.
// say "in goFoodDir";
log "in goFoodDir";
let d = fst r in
if (d == down) {
foodHere <- ishere "oats";
Expand All @@ -26,14 +15,14 @@ def goFoodDir = \f. \r.
// navigation direction is determined
// but before we move.
try {
moveWithMortality;
move;
} {};
f;
}
end;

def goHomeDir = \f. \r.
// say "in goHomeDir";
log "in goHomeDir";
let d = fst r in
if (d == down) {
return ()
Expand All @@ -44,14 +33,14 @@ def goHomeDir = \f. \r.
// navigation direction is determined
// but before we move.
try {
moveWithMortality;
move;
} {};
f;
}
end;

def findGoodDirection =
// say "in findGoodDirection";
log "in findGoodDirection";
isBlocked <- blocked;
if isBlocked {
turn left;
Expand All @@ -63,7 +52,7 @@ def moveUntilBlocked =
isBlocked <- blocked;
if isBlocked {
} {
moveWithMortality;
move;
moveUntilBlocked;
};
end;
Expand All @@ -77,21 +66,20 @@ def pauseAtRandom =
end;

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

def pursueFood = \hadSensedFood. \homeLoc.
// say $ "in pursueFood. hadSensedFood? " ++ format hadSensedFood;
log $ "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;
log "in doMovement";

findGoodDirection;
moveUntilBlocked;
Expand All @@ -106,7 +94,6 @@ def go = \startLoc.
end;

startLoc <- whereami;
// let shouldDestruct = !(snd startLoc == -17 && fst startLoc < 19) in
let shouldDestruct = false in
if shouldDestruct {
selfdestruct;
Expand Down
133 changes: 118 additions & 15 deletions data/scenarios/Challenges/_pied-piper/solution.sw
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
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 uTurn = \d.
turn d;
move;
turn d;
end;

/**
Starting at bottom right of field
*/
def harvestField = \fieldWidth.
intersperse fieldWidth move harvest;
uTurn right;
intersperse fieldWidth move harvest;
uTurn left;
intersperse fieldWidth move harvest;
uTurn right;
intersperse fieldWidth move harvest;
end;

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


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

Expand All @@ -21,29 +49,104 @@ one crumb short of the first crumb.
def makeOatsTrail = \spacing. \segments.
doN segments $ makeOatsCrumb spacing;
turn back;
doN ((segments - 1)*spacing) move;
doN (segments * spacing) move;
turn back;
end;

def placeHorizontalTrail = \horizontalSpacing.
turn east;
doN (2*horizontalSpacing) move;
place "oats";
turn back;
doN horizontalSpacing move;
place "oats";
doN horizontalSpacing move;
turn left;
end;

def waitForRatToPass =

// Wait until the rat ate this crumb
watch down;
wait 2000;
waitUntilRatDisappeared;
end;

def go =
def makeTrails =
let spacing = 4 in
let segments = 5 in

wait 30;
placeHorizontalTrail 5;
makeOatsTrail spacing $ segments + 1;
end;

def getKey =
turn east;
doN 10 move;
place "oats";
turn back;
doN 5 move;
place "oats";
doN 15 move;
turn right;
move;
k <- grab;
equip k;
turn right;
doN 5 move;
turn right;
doN 6 move;
turn left;
doN 18 move;
turn left;
doN 20 move;
turn right;
doN 2 move;
use "key" forward;
doN 6 move;
turn right;
doN 8 move;
turn left;
move;
turn right;
doN 6 move;
turn left;
move;
end;

def go =
getKey;
harvestField 20;
move;
turn right;
doN 9 move;
turn left;
move;
turn right;
doN 8 move;
turn left;
doN 8 move;
turn left;

// Start laying trail
intersperse 5 (doN 4 move) $ place "oats";
placeHorizontalTrail 5;

log "Here A";


waitForRatToPass;

makeTrails;
log "Here B";
waitForRatToPass;

log "Here C";
turn east;
doN 2 move;

placeHorizontalTrail 4;
makeOatsTrail 4 4;
waitForRatToPass;

makeOatsTrail spacing segments;
// Has probably awakened due to rat eating food here.
waitUntilDisappeared;
makeOatsTrail spacing $ segments;
makeOatsTrail 4 6;
placeHorizontalTrail 4;
end;

go;
10 changes: 2 additions & 8 deletions data/scenarios/Challenges/pied-piper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,7 @@ world:
- src: block
offset: [-10, -14]
- src: block
offset: [-16, -30]
orient:
up: east
- src: block
offset: [6, -30]
orient:
up: east
offset: [-10, -36]
- src: storage silo
offset: [8, 9]
- src: field
Expand Down Expand Up @@ -373,7 +367,7 @@ world:
, mask (x == -7 && y >= 3 || y == 3 && x <= -7) {fence}
, mask (y > -2 && y < 2) {water}
, mask (x > -3 && x < 3) {stone}
, mask (y > -35 && y < -29) {stone}
, mask (y > -25 && y < -19) {stone}
]
map: |
bbbbb
Expand Down

0 comments on commit 7eec7ea

Please sign in to comment.