Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

ipad

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?

like image 644
Cœur Avatar asked Nov 30 '25 07:11

Cœur


2 Answers

I do not understand the Playground's instruction counter. It does not complain about instruction count for this:

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

func btl() {
  bounce()
  turnLeft()
}

func b() {
  btl()
  btl()
}

b()
b()

Perhaps in the Playground, function-call overhead is negative. Stop unrolling those loops!

like image 113
Thomas L Holaday Avatar answered Dec 03 '25 03:12

Thomas L Holaday


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

like image 40
SimpleBeat Avatar answered Dec 03 '25 05:12

SimpleBeat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!