Skip to content

Commit

Permalink
rat program
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Sep 1, 2024
1 parent 126b51c commit 1043b7a
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 31 deletions.
84 changes: 78 additions & 6 deletions data/scenarios/Challenges/_pied-piper/rat.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
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 {
move;
} {};
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 {
move;
} {};
f;
}
end;

def findGoodDirection =
// say "in findGoodDirection";
isBlocked <- blocked;
if isBlocked {
turn left;
Expand All @@ -23,15 +65,45 @@ def pauseAtRandom =
} {}
end;

def doMovement =
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 checkExpireSelf =
loc <- whereami;
if (snd loc > 6) {selfdestruct} {};
end;

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

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

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

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

def makeOatsTrail =
place "oats";
doN 4 move;
end;

def go =
place "tree";
move; move;
// place "oats";
doN 5 makeOatsTrail;
turn back;
doN (5*4) move;
turn back;
end;

wait 10;
go;
44 changes: 23 additions & 21 deletions data/scenarios/Challenges/pied-piper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,33 @@ terrains:
Wooden raised path
robots:
- name: base
loc: [18, -22]
dir: north
loc: [19, 7]
dir: south
devices:
- 3D printer
- ADT calculator
- antenna
- branch predictor
- clock
- comparator
- compass
- counter
- dictionary
- grabber
- hearing aid
- lambda
- logger
- net
- scanner
- strange loop
- string
- treads
- workbench
inventory:
- [50, oats]
- [50, tree]
- [200, oats]
walkable:
never:
- door
- name: rat
system: true
dir: west
Expand All @@ -49,16 +54,6 @@ robots:
- logger
program: |
run "scenarios/Challenges/_pied-piper/rat.sw"
- name: helper
loc: [0, 10]
system: true
dir: west
display:
invisible: false
devices:
- logger
program: |
noop;
objectives:
- goal:
- Rats have infested the village dwellings. Clear them out!
Expand All @@ -72,10 +67,17 @@ solution: |
entities:
- name: door
display:
char: 'X'
char: 'D'
description:
- door
properties: [known]
- name: wall
display:
char: 'W'
attr: wood
description:
- wall
properties: [known, unwalkable]
- name: oats
display:
attr: oats
Expand All @@ -93,7 +95,7 @@ world:
structure:
palette:
'd': [stone, door]
'x': [stone, boulder]
'x': [stone, wall]
'.': [stone]
'r': [stone, erase, rat]
map: |
Expand Down Expand Up @@ -150,8 +152,8 @@ world:
, mask (y > -37 && y < -32) {stone}
]
map: |
....bbbb..
....bbbb..
....bbbb..
....bbbb..
....bbbb..
....bbbb
....bbbb
....bbbb
....bbbb
....bbbb

0 comments on commit 1043b7a

Please sign in to comment.