Skip to content

Instantly share code, notes, and snippets.

@imbradmiller
Last active December 10, 2023 21:05
Show Gist options
  • Save imbradmiller/438fa0459ef5bc14cef77ee1f203ecb5 to your computer and use it in GitHub Desktop.
Save imbradmiller/438fa0459ef5bc14cef77ee1f203ecb5 to your computer and use it in GitHub Desktop.
Random Gems Everywhere
let totalGems = randomNumberOfGems
var gemCon = 0
func navigate () {
while !isBlocked {
moveForward()
if isOnGem {
collectGem()
gemCon += 1
}
}
}
func turnOffPortals() {
bluePortal.isActive = false
pinkPortal.isActive = false
}
func turnOnPortals() {
bluePortal.isActive = true
pinkPortal.isActive = true
}
func turnAround() {
turnRight()
turnRight()
}
while gemCon < totalGems {
navigate()
turnAround()
turnOffPortals()
navigate()
turnAround()
turnOnPortals()
}
@Twiinner
Copy link

I think it is a must to break down the problem into smaller one, in this case, solve the side, then go for the middle one (seems like the puzzle itself will not spawn gem on the edge, just the straight line one, which means the puzzles for the side and middle one are the same). Use variable to keep tracking what side are you on, then make sure your character will move across that path and collect gem on that path if existed.

This puzzle is a fundamental one, where you should think about the problem itself as multiple small problems. And use the variable to keep tracking the status of the puzzle.

In case you wonder about the portal, just imagine how would you take that path, then write code to toggle the portal as you need it to be.

@uniqueariyan
Copy link

This is simple but cleaver soloution
Also If you add FUNC to it,it will be much better,
anyway:

let totalGems = randomNumberOfGems

var gem = 0

while gem < totalGems {
moveForward()
if isOnGem {
collectGem()
gem += 1
}
if isBlocked {
turnRight()
turnRight()
if pinkPortal.isActive == false {
pinkPortal.isActive = true
bluePortal.isActive = false
}else{
pinkPortal.isActive = false
bluePortal.isActive = true
}
}
}

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