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:
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
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 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
F1 was very short. In
I turned 180 degree back,
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
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
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.