I have a problem with solving the "Dispersion V2" puzzle on robozzle.com:

Dispersion V2

The puzzle has a robot that can be moved and turned with a program. The goal of the puzzle is to have the robot visit all cells that have a star -- in this case just one.

The robot can perform actions unconditionally or conditional on a certain cell color.

This particular level looks like a rectangular spiral, the robot's initial position at bottom-left.

  • Every side of the spiral has 4 blocks of red;
  • Every edge(turn) is red block
  • Every side of the spiral is shorter than prev.side by 1 block

I need to write an algorithm using 4 functions only, named F1, F2, F3, and F4. Each function can only perform 2 commands, except F2 which can do 3 commands. Here you see the empty template for the program (the greyed numbers are just placeholders where the actual commands must be placed):

4 functions with commands

The available commands that these functions can use are:

  • : move forward
  • : turn left
  • : turn right
  • Fx: call function (either F1, F2, F3, F4). Recursion is fine.


These commands can be accompanied by a color or not. When they have a color, they only have effect when the current cell (having the robot) has that same color. When it is different, the command is ignored, and execution continues with the next command.

Once the puzzle is solved the program will halt (no matter how deep the call stack is) with success. If the robot is steered off the grid, the program will halt with a failure.

What I have now:

current algo

How I can solve this puzzle?


1 Answer 1


Your attempt has no main loop: it has a loop over blue cells, but then calls F3 and F2 will never be run again.

What you did in F2 is a good start: moving along as long as squares are blue. But I would do that in one of the other functions that only has 2 entries. More, I would not make the move conditional; only the repetition of it should be conditional on blue. That way you can also call it when you're on a red spot:

F4: ↑ F4(blue)

So the action F4 will bring you to a non-blue spot, but if you start on a non-blue spot, it will bring you to the next one.

We need to have this happen four times. Well, 4 is 2x2, so let's first repeat this twice in the function F3:

F3: F4 F4

... and repeat F3 twice so that we will have moved to the fourth non-blue spot. That's where we need to make a turn:

F2: F3 F3 ↰

So, F2 will bring us to the fourth red spot and turn. This is should be repeated endlessly:

F1: F2 F1

And that's it!

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.