Cargo-Bot: Help my logic

Puzzle: #11 Color Sort (Easy)

I’m starting this discussion topic, which I may need to revisit for some further help with my logic for other puzzles.

Maybe I’m not understanding how each tool works OR maybe this is a glitch…

My sequence so far seems to repeat a step that it shouldn’t. Without an instruction to move I expect the sequence to stop however it goes back one register and repeats. I don’t understand why?

Please, I don’t want to look at the full solution yet. I only want to first understand what is wrong with my logic sequence before I finish perfect and earn my 3 stars!

At the moment it looks like this:

Prog1. Down redLeft redP2 greenRight Down Left

Prog2. Down Right P1

When I step through the sequence this is what happens:

First 2 red boxes are deposited in the first column. The green box is put in the right column and the hoist backs up to centre - then it should stop but it doesn’t.

It goes back one register and picks up the third red box reusing the 5th register in the sequence then continues to the 6th register (move left), then repeats 5th again (drop box) and 6th again move left - smash!

Why is it doing this loop?

*edit: I got my 3 stars - 9 registers :slight_smile:

Glad to hear you got your 3 stars :slight_smile:

On your question - it will execute the end of P1 3 times because P1 itself got called 3 times. Once at the start, and once each time that P2 got executed. Each time P1 gets called, the whole of P1 is executed.

This is actually a useful trick for many of the later levels, as it allows you to execute a loop a certain number of times, so in effect you can use it to count stuff.

I think the best way to explain is to simulate what the program looks like at each step and write all the instructions that are scheduled to be executed at each point

  1. P1 (every program starts like this. Next step: replace P1 with its instructions)

  2. down, left/red, p2/red, right/green, down, left (execute the down move)

  3. left/red, p2/red, right/green, down, left (execute the left move)

  4. p2/red, right/green, down, left (just like with p1 at the start, p2 is called, so we replace p2 with its instructions)

  5. (down, right, p1), right/green, down, left

  6. right, p1, right/green, down, left

  7. p1, right/green, down, left (p1 is called so we replace p1 with its instructions)

  8. (down, left/red, p2/red, right/green, down, left), right/green, down, left

  9. left/red, p2/red, right/green, down, left, right/green, down, left

  10. p2/red, right/green, down, left, right/green, down, left

  11. (down, right, p1), right/green, down, left, right/green, down, left

  12. right, p1, right/green, down, left, right/green, down, left

  13. p1, right/green, down, left, right/green, down, left

  14. (down, left/red, p2/red, right/green, down, left), right/green, down, left, right/green, down, left

  15. left/red, p2/red, right/green, down, left, right/green, down, left, right/green, down, left

  16. p2/red, right/green, down, left, right/green, down, left, right/green, down, left

  17. (right/green, down, left), right/green, down, left, right/green, down, left

  18. down, left, right/green, down, left, right/green, down, left

  19. left, right/green, down, left, right/green, down, left

  20. right/green, down, left, right/green, down, left (here’s where you thought it would stop)

  21. down, left, right/green, down, left

  22. left, right/green, down, left

  23. right/green, down, left

  24. down, left

  25. left

(end)

Aha - I see now… it continues the line of instruction. Very useful to know. Thanks Ruilov.

Seeing this made me realise that Cargo-Bot’s language is quite TeX-like. The programs aren’t like functions, they are more like macros. So the processor sees “P2” and “expands” that to the contents of “P2”.

I’ll have to write a TeX-version of Cargo-Bot …