swiftipad

Playgrounds "Collect, Toggle, Repeat" in 10 basic commands or less?


iPad Playgrounds app, very early challenge: you're learning programming, you don't know anything about variables, loops, etc. The only keyword you know is func. The game advices you to build your own function for the first time.

challenge start

Basically, you're only going to use what's on screen: collectGem(), moveForward(), name(), toggleSwitch(), turnLeft(), turnRight(). You won't fall accidentally (so extra moves are acceptable), and the goal is to collect 4 gems and toggle 4 switches.

My first attempt was:

challenge end

Puzzle is solved, but tells me:

but you used 11 commands! Try defining your own function [...] You won't need to use as many commands

Unfortunately, I can't figure out how to use less commands with just the func keyword. Is it possible? (note that I already figured out that using loops is cheating)

Also, are there places to discuss about Playgrounds puzzles?


Solution

  • 11 calls is enough indeed!

    (but function names and structure of the code should be of a certain style)

    If you're to take Thomas L Holaday's answer and to rename the functions (for example instead of bounce() you'd put clear()), the solution no longer validates! Same with the name of the b() function!

    After tinkering with the code and function names I found it is possible to code it with 11 calls, which is almost the same as original, and pass validation!

    func bounce() {
      moveForward()
      collectGem()
      moveForward()
      toggleSwitch()
      moveForward()
      moveForward()
      turnLeft()
    }
    
    func b() {
      bounce()
      bounce()
    }
    
    b()
    b()
    

    Here is the screen of my solution

    It seems that the problem here is not with the solutions (which are correct!) but with the validator incorrect way of checking for the naming of the functions and the structure of the code (that little optimization with b() function must be present, as well as the "correct" function names).