Bones All the Way
There's a bone on every tile between the dog and the goal, as in A Bone on Every Tile. But the corridors have different lengths, so this time you need Repeat until at the goal.
The program that comes to mind first is the one from A Bone on Every Tile, in the new loop:
Repeat until at the goalMove forwardPick up bone
Go through its last pass in your head. The dog stands on the last bone, which it has just picked up. It isn't at the goal yet, so the loop runs one more pass: the dog steps onto the goal and tries to pick up a bone there. But there's no bone on the goal, so the program stops with an error.
The loop checks whether the dog is at the goal only before each pass. So in every pass, the step onto the next tile has to come last. When that step takes the dog to the goal, the loop ends before the dog tries to pick anything up. Each pass then does this: the dog picks up the bone it's standing on and steps forward.
That fixes the last pass, but it breaks the first one: at the start, the dog isn't standing on a bone. What does it have to do before the loop? The first and the last pass of a loop are often different from the others. That's why programmers always go through them in their heads.
Instructions
- 1
Pick up all the bones on all three maps.
- 2
Walk to the goal.