Skip to content

Instantly share code, notes, and snippets.

@benjaminpadula
Last active September 15, 2022 07:11
Show Gist options
  • Save benjaminpadula/f32b624f132342a436ac83f46e8b4d13 to your computer and use it in GitHub Desktop.
Save benjaminpadula/f32b624f132342a436ac83f46e8b4d13 to your computer and use it in GitHub Desktop.
Swift Playgrounds: Roll Right, Roll Left Solution
while !isOnOpenSwitch {
while !isBlocked {
if isOnGem {
collectGem()
}
if isOnClosedSwitch {
toggleSwitch()
}
moveForward()
}
if isBlockedRight || isOnClosedSwitch {
turnLeft()
}
if isBlockedLeft || isOnGem {
turnRight()
}
}
@benjaminpadula
Copy link
Author

From Swift Playgrounds Learn to Code final challenge. I searched online, but didn’t find any elegant solutions, so I decided to post my own. I’d be happy to hear your thoughts/suggestions/questions.

@javierlosada
Copy link

Great solution. You could simplify by removing the last isOnGem since all gems are in a corner.

if isBlockedLeft { turnRight() }

@NPSoftware68
Copy link

great work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment