Swift — It Takes Two

This was a challenging level which initially stumped me when I tried to puzzle it out in my head before writing a line of code.

To overcome this, I resigned to first just write out every single action line by line and then reviewed my code to look for patterns — which I then turned in to functions. My code came out to 49 lines (below).

I then searched for another solution that may be more efficient.

This one was built in 33 lines — and reminded me to utilize for i in 1…X with a puzzle like this one that has a known area to navigate.

Objective: Create two monsters; one to turn locks for moving platforms up or down, and the other to do actions on each — get gem or toggle switch.
0390972D-55FF-4068-B83F-AF3F33725EC0.png

let expert = Expert()
let character = Character()
var itemCount = 0
func gemSwitch() {
character.moveForward()
character.moveForward()
if character.isOnGem {
itemCount += 1
character.collectGem()
}
if character.isOnClosedSwitch {
itemCount += 1
character.toggleSwitch()
}
}
func lockTurn() {
expert.turnLeft()
if itemCount == 0 {
expert.turnLockDown()
expert.turnLockDown()
}
if itemCount == 1 {
expert.turnLockUp()
}
gemSwitch()
}
func rightMove() {
expert.turnRight()
expert.moveForward()
expert.moveForward()
}
func moveFour() {
expert.moveForward()
expert.moveForward()
expert.moveForward()
expert.moveForward()
}
expert.turnLeft()
while itemCount <= 1 {
moveFour()
rightMove()
lockTurn()
rightMove()
rightMove()
moveFour()
rightMove()
lockTurn()
}

 
27
Kudos
 
27
Kudos

Now read this

“Everything I know about business in one minute.”

by William Drenttel Focusing on making a partnership work is more profitable than focusing on making money. Love your employees more than you love your clients.  The best new business is your current business. Price projects by... Continue →