-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
796 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ capture.yaml | |
powerset.yaml | ||
fishing.yaml | ||
gated-paddock.yaml | ||
pied-piper.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
def moveWithMorbidity = | ||
let mold = "mold" in | ||
moldHere <- ishere mold; | ||
if moldHere { | ||
try { | ||
// handle race conditions in which | ||
// another robot grabs it first | ||
m <- harvest; | ||
if (m == mold) { | ||
say $ "Yuck, " ++ mold ++ "! I'm outta here."; | ||
selfdestruct; | ||
} {}; | ||
} {}; | ||
} {}; | ||
|
||
move; | ||
end; | ||
|
||
def goFoodDir = \f. \r. | ||
log "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 { | ||
moveWithMorbidity; | ||
} {}; | ||
f; | ||
} | ||
end; | ||
|
||
def goHomeDir = \f. \r. | ||
log "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 { | ||
moveWithMorbidity; | ||
} {}; | ||
f; | ||
} | ||
end; | ||
|
||
def findGoodDirection = | ||
log "in findGoodDirection"; | ||
isBlocked <- blocked; | ||
if isBlocked { | ||
turn left; | ||
findGoodDirection; | ||
} {}; | ||
end; | ||
|
||
def moveUntilBlocked = | ||
isBlocked <- blocked; | ||
if isBlocked { | ||
} { | ||
moveWithMorbidity; | ||
moveUntilBlocked; | ||
}; | ||
end; | ||
|
||
def pauseAtRandom = | ||
r <- random 3; | ||
if (r == 0) { | ||
r2 <- random 12; | ||
wait $ 6 + r2; | ||
} {} | ||
end; | ||
|
||
def returnHome = \homeLoc. | ||
log "in returnHome"; | ||
nextDir <- path (inL ()) (inL homeLoc); | ||
case nextDir return $ goHomeDir $ returnHome homeLoc; | ||
end; | ||
|
||
def pursueFood = \hadSensedFood. \homeLoc. | ||
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. | ||
log "in doMovement"; | ||
|
||
findGoodDirection; | ||
moveUntilBlocked; | ||
pauseAtRandom; | ||
pursueFood false startLoc; | ||
end; | ||
|
||
/** Loop */ | ||
def go = \startLoc. | ||
doMovement startLoc; | ||
go startLoc; | ||
end; | ||
|
||
startLoc <- whereami; | ||
let shouldDestruct = false in | ||
if shouldDestruct { | ||
selfdestruct; | ||
} { | ||
go startLoc; | ||
} |
191 changes: 191 additions & 0 deletions
191
data/scenarios/Challenges/Ranching/_pied-piper/solution.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
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 waitUntilRatDisappeared = | ||
found <- scout east; | ||
if found { | ||
wait 1; | ||
waitUntilRatDisappeared; | ||
} {}; | ||
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 * 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 makeTrails = | ||
let spacing = 4 in | ||
let segments = 5 in | ||
|
||
wait 30; | ||
placeHorizontalTrail 5; | ||
makeOatsTrail spacing $ segments + 1; | ||
end; | ||
|
||
def getKey = | ||
turn east; | ||
doN 15 move; | ||
turn right; | ||
move; | ||
_k <- grab; | ||
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 "unlocker" forward; | ||
doN 6 move; | ||
turn right; | ||
doN 8 move; | ||
turn left; | ||
move; | ||
turn right; | ||
doN 6 move; | ||
turn left; | ||
move; | ||
end; | ||
|
||
def placeMold = | ||
turn east; | ||
doN 11 move; | ||
sow "mold"; | ||
end; | ||
|
||
def go = | ||
getKey; | ||
harvestField 20; | ||
|
||
turn left; | ||
doN 2 move; | ||
turn left; | ||
harvestField 20; | ||
|
||
|
||
move; | ||
turn right; | ||
doN 14 move; | ||
turn left; | ||
|
||
// Get the mold | ||
doN 4 move; | ||
turn left; | ||
doN 3 move; | ||
harvest; | ||
turn back; | ||
doN 3 move; | ||
|
||
turn right; | ||
doN 3 move; | ||
turn left; | ||
|
||
// Head back to the house | ||
doN 8 move; | ||
turn left; | ||
doN 8 move; | ||
turn left; | ||
|
||
// Start laying trail | ||
intersperse 5 (doN 4 move) $ place "oats"; | ||
placeHorizontalTrail 5; | ||
|
||
waitForRatToPass; | ||
|
||
makeTrails; | ||
waitForRatToPass; | ||
|
||
|
||
makeOatsTrail 4 10; | ||
placeHorizontalTrail 5; | ||
waitForRatToPass; | ||
|
||
makeOatsTrail 4 12; | ||
placeHorizontalTrail 5; | ||
waitForRatToPass; | ||
|
||
turn east; | ||
doN 2 move; | ||
|
||
placeHorizontalTrail 4; | ||
makeOatsTrail 4 4; | ||
waitForRatToPass; | ||
|
||
makeOatsTrail 4 6; | ||
placeHorizontalTrail 4; | ||
|
||
waitForRatToPass; | ||
makeOatsTrail 4 10; | ||
placeHorizontalTrail 4; | ||
|
||
|
||
waitForRatToPass; | ||
makeOatsTrail 4 12; | ||
placeHorizontalTrail 4; | ||
|
||
placeMold; | ||
end; | ||
|
||
go; |
Oops, something went wrong.