Created
September 26, 2016 13:01
-
-
Save imbradmiller/9b7944323e953b0406f9914e324bc67b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while !isBlocked { | |
moveForward() | |
if isOnGem { | |
collectGem() | |
turnRight() | |
moveForward() | |
collectGem() | |
} else if isOnClosedSwitch && isBlockedLeft { | |
toggleSwitch() | |
turnRight() | |
} else if isOnClosedSwitch && isBlockedRight { | |
toggleSwitch() | |
turnLeft() | |
moveForward() | |
toggleSwitch() | |
} else if isBlocked && isBlockedRight { | |
turnLeft() | |
} | |
} |
Yeah. That solution won’t work. Here’s what I came up with. I like this one because there are no repeated commands, just one collectGem()
, one toggleSwitch()
, one moveForward()
, one turnLeft()
, one turnRight()
. Makes it easier to read for me and fewer lines of code:
while !isOnOpenSwitch {
while !isBlocked {
if isOnGem {
collectGem()
}
if isOnClosedSwitch {
toggleSwitch()
}
moveForward()
}
if isBlockedRight || isOnClosedSwitch {
turnLeft()
}
if isBlockedLeft || isOnGem {
turnRight()
}
}
So funny how many options there are to solve this puzzle! This one took me so long to figure out (more than what I'd like to admit), and I felt oddly relieved after I saw the code worked.
Here's what I came up with:
func navAround() {
if !isBlocked {
while isOnGem || isOnClosedSwitch {
if isOnGem {
collectGem()
} else if isOnClosedSwitch {
toggleSwitch()
}
}
moveForward()
} else if isBlockedLeft && isBlocked {
turnRight()
} else {
turnLeft()
}
}
while !isOnOpenSwitch {
navAround()
}
Cheers!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution for Roll Left, Roll Right tutorial in Swift Playgrounds.
Man this shiz was hard because I was overcomplicating it. It was very simple and cleaner this way.