Turn Until the Path Is Ahead
This time, the dog starts in a dead end, facing a hedge. The way out is on its left on the first map, on its right on the second, and behind it on the third. The dog has to turn to face the way out and then walk to the goal.
What should it do? Turn as long as there's no path ahead:
Repeat whilenotpath aheadTurn left ↺
Before every pass, the dog looks ahead. While there's a hedge in front of it, it turns a quarter turn. As soon as it faces the path, the loop ends. On the first map, the dog turns once, on the third twice, and on the second three times: three quarter turns to the left leave it facing where one turn to the right would. The loop doesn't need to know where the way out is. The dog keeps turning until it faces it.
Without not, the dog would have to ask about each direction: if there's a path to the left, turn left, else if there's a path to the right, turn right, and else turn around. That's two questions and eight blocks. The loop asks one question and has four blocks: Repeat while, not, path ahead and Turn left ↺.
Then the dog walks to the goal. Look closely: the path goes on past the goal. So the dog has to stop at the goal, and Repeat while path ahead would take it past. Which loop stops at the goal?
Instructions
- 1
Get the dog to the goal on all three maps: turn it with
Repeat whilenotpath ahead, then walk to the goal.