8
$\begingroup$

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.

robozzle puzzle 132 "split" blank

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.

robozzle "split" with commands

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

robozzle stairs blank

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.

Robozzle Stairs with commands but no iteration or recursion

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.

stairs completed

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).

Split long solution

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.

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

4 Answers 4

13
$\begingroup$

7 commands:

Command image: F1=F2,F1; F2=Step,Green-Right,Blue-F2,Right,Right

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.

$\endgroup$
1
  • $\begingroup$ just wow. Thanks for the clever and shortest solution. Never thought that way. $\endgroup$
    – garakchy
    Jan 28, 2023 at 7:02
5
$\begingroup$

Here is another 9-command solution:

$\endgroup$
0
5
$\begingroup$

Below is a 9-command solution.

enter image description here

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

enter image description here

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

enter image description here

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

$\endgroup$
2
  • $\begingroup$ Colour-independent right turn is nice. Thanks for the solution. $\endgroup$
    – garakchy
    Jan 28, 2023 at 7:12
  • $\begingroup$ 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. $\endgroup$
    – garakchy
    Jan 31, 2023 at 2:08
1
$\begingroup$

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

enter image description here

To this:

enter image description here

$\endgroup$
1
  • $\begingroup$ 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 $\endgroup$
    – garakchy
    Feb 9, 2023 at 18:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.