# Shorter solution for Robozzle puzzle no 132 - Split

I wonder a shorter solution for Robozzle puzzle number 132, "Split."

Puzzle is shown below. I have solved it but I'm wondering shorter solution for this and there many exist.

Basically, the rocket has to collect stars.

You give certain commands to the rocket and it obeys them. The less command, the better. Commands for this puzzle are shown below.

Commands are displayed on bottom right corner. From top to down they are: go straight, turn left, turn right; execute F1 function, execute F2 function; execute this command no matter what the colour of cell is, execute this command in red cell and execute this command in green cell.

Each time it starts with F1 function. If you don't call F2 function in F1 then it will not execute commands in F2.

Let me show you how to play this game in another easy puzzle.

This puzzle is called "Stairs." You can see the commands and given function on the bottom right corner again. They are blank. If I click on Go! nothing happens because there are no commands given in function.

Now, let's fill in the function with some necessary commands.

You can see where robot stays. It hasn't collected all the stars. Function was called but it was executed just once. Why? Because there is no iteration (or recursion) or repetition of commands.

Remember we need to collect all the stars, so let's call F1 function again at the end of commands so that it iterates (or recurses) or repeats itself.

The rocket has collected all the stars because F1 was called again at the end so that it repeatedly goes beginning when it finishes executing all the commands. At the bottom you can see what the next steps are such as turn left, go, turn right and F1. This was completed in 5 commands (or steps).

Now, hoping that you understood the fundamentals of this puzzle let me show you my longer solution for Robozzle puzzle number 132 - "Stairs". It contains 11 commands (or steps).

I screenshot it during the execution of commands so that you can see the upcoming steps (or commands) on bottom command line. I directly called F2 because F1 was very short. In F2 I turned 180 degree back, stepped forward, turned 180 degree back again, and stepped forward twice. I repeated these steps as long as the cells were blue. After blue cells I put turn right on green and go straight on any colour.

Basically, I stepped back once and stepped forward twice. Repeated this action (or recalled F2) as long as the rocket was in blue cells. Since there were a lot of blue cells, the F2 called itself again and again postponing the commands (or steps) I entered after F2 there: turn right on green and go straight on any colour. As far as I know this is called "stacks" (or "stacking") in programming. You can see them piled up on the command line at the bottom of the image. When the rocket arrives at the green cell it gives up calling F2 and executes the stack or piled steps after F2: it turns right on the green cell and goes straight. It ignores turn right on green afterwards. Why? Because there are no green cells except the corner.

My solution contains 11 commands (or steps). I'm looking for a shorter solution for this Robozzle "Split" puzzle: http://www.robozzle.com/js/play.aspx?puzzle=132.

There exist 7 command (or 7 step) solution.

• Suppose a command would take the robot off the coloured squares. Is that a not allowed, or would it simply be ignored? Jan 27, 2023 at 16:21
• @JaapScherphuis A move that takes the robot off the colored squares (is not ignored or prevented, and) fails the task.
– Rubio
Jan 27, 2023 at 16:45

7 commands:

Explanation:

The function F2 is called repeatedly. F2 steps forward N times until not on a blue position, turns right if the end position is green, then does a $$180^\circ$$ turn N times. F2 uses recursion to count the number of steps N.

In the first execution, N=8. Since this is even, the $$180^\circ$$ steps do nothing in total, and with the green right turn, the bot ends facing right. In the second and third executions, N=5. Since this is odd, the $$180^\circ$$ steps are equivalent to one $$180^\circ$$ turn. So the bot turns $$180^\circ$$ at the red square, and ends facing left. After returning to the green square, the bot does the green right turn followed by a $$180^\circ$$ turn, and in total turns from left to down. The fourth execution of F2 finds the second star, winning.

• just wow. Thanks for the clever and shortest solution. Never thought that way. Jan 28, 2023 at 7:02

Here is another 9-command solution:

Below is a 9-command solution.

And here is a more elegant recursive 10-command solution.

But what you wanted is a 7-command solution. Here it is:

... but as I post this I see aschepler posted it hours ago...

• Colour-independent right turn is nice. Thanks for the solution. Jan 28, 2023 at 7:12
• Coming up with 3 different solutions, wow! Thanks for your efforts! And 10-command solution, it took me a while to understand (even though I was solving these types of puzzles for a long time) the double iteration and stacks, especially the left turn on green cell left behind after the second F2 (green) call. Nice one! Well done! Thanks again. Jan 31, 2023 at 2:08

Really cool game. My solution isn't that elegant, but hey. Managed to whittle down this:

To this:

• One less right turn and one less move forward; very good. Thank you @Nautilus for your solution and efforts. By the way, I was on this website whole day but I didn't get notification that this answer was added, I don't know why. Just accidentally saw your answer when I checked my own question. Thanks again. I'm glad that you liked the Robozzle game Feb 9, 2023 at 18:58