-
-
Save imbradmiller/438fa0459ef5bc14cef77ee1f203ecb5 to your computer and use it in GitHub Desktop.
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() | |
} |
I found this to be the solution from improving his end code:
let totalGems = randomNumberOfGems
var collectedGems = 0
func turnAround() {
turnLeft()
turnLeft()
}
func turnOffPortals() {
pinkPortal.isActive = false
bluePortal.isActive = false
}
func turnOnPortals() {
pinkPortal.isActive = true
bluePortal.isActive = true
}
func navigate() {
while !isBlocked {
moveForward()
if isOnGem {
collectGem()
collectedGems += 1
}
}
}
while collectedGems != totalGems {
turnOffPortals()
navigate()
turnAround()
navigate()
turnAround()
turnOnPortals()
navigate()
}
to navigate the ends of the middle section make this slight adjustment:
func navigate() {
while !isBlocked {
moveForward()
collectGems()
if !isBlockedLeft {
turnLeft()
}
}
}
I found this solution, let me know if this code is not efficient:
let totalGems = randomNumberOfGems
var gemCounter = 0
func portalToggler() {
if pinkPortal.isActive == true {
pinkPortal.isActive = false
bluePortal.isActive = false
} else {
pinkPortal.isActive = true
bluePortal.isActive = true
}
}
func solveSideBit() {
while !isBlocked{
if !isOnGem {
moveForward()
} else {
collectGem()
gemCounter += 1
}
}
if isOnGem {
collectGem()
gemCounter += 1
}
turnLeft()
turnLeft()
moveForward()
}
func sidesolver() {
bluePortal.isActive = true
pinkPortal.isActive = true
moveForward()
solveSideBit()
portalToggler()
solveSideBit()
portalToggler()
moveForward()
}
func TSolver() {
portalToggler()
for i in 1 ... 7 {
if isOnGem {
collectGem()
gemCounter += 1
}
if isBlockedLeft && isBlocked && isBlockedRight{
turnLeft()
turnLeft()
moveForward()
} else if isBlocked && !isBlockedLeft{
turnLeft()
moveForward()
} else if isBlockedRight && isBlockedLeft {
moveForward()
} else if isBlockedRight{
turnLeft()
moveForward()
} else {
moveForward()
}
}
}
while gemCounter < totalGems {
sidesolver()
TSolver()
}
Here is my solution:
let totalGems = randomNumberOfGems
var gemsCollected = 0
var exploreCount = 0
var upperLevelCount = 0
func collectAndCount(){
if isOnGem {
collectGem()
gemsCollected += 1
}
}
func explore () {
collectAndCount()
turnLeft()
moveForward()
collectAndCount()
turnLeft()
turnLeft()
moveForward()
moveForward()
collectAndCount()
turnRight()
turnRight()
moveForward()
turnLeft()
moveForward()
}
func portalSwitches() {
if exploreCount == 2 && upperLevelCount < 1 {
pinkPortal.isActive = true
} else if exploreCount == 2 && upperLevelCount == 1 {
pinkPortal.isActive = false
} else if exploreCount == 2 && upperLevelCount == 2 {
pinkPortal.isActive = true
} else if exploreCount == 3 && upperLevelCount == 2 {
pinkPortal.isActive = false
bluePortal.isActive = true
} else if exploreCount == 3 && upperLevelCount == 3 {
bluePortal.isActive = false
}
}
pinkPortal.isActive = false
bluePortal.isActive = false
while gemsCollected < totalGems {
collectAndCount()
portalSwitches()
if isBlocked && !isBlockedLeft && !isBlockedRight {
exploreCount += 1
portalSwitches()
explore()
} else if isBlocked && isBlockedLeft {
turnLeft()
turnLeft()
upperLevelCount += 1
portalSwitches()
} else {
moveForward()
}
}
I found this solution, let me know if this code is not efficient:
let totalGems = randomNumberOfGems var gemCounter = 0
func portalToggler() {
if pinkPortal.isActive == true { pinkPortal.isActive = false bluePortal.isActive = false } else { pinkPortal.isActive = true bluePortal.isActive = true }
}
func solveSideBit() { while !isBlocked{ if !isOnGem { moveForward() } else { collectGem() gemCounter += 1 }
} if isOnGem { collectGem() gemCounter += 1 } turnLeft() turnLeft() moveForward()
}
func sidesolver() { bluePortal.isActive = true pinkPortal.isActive = true moveForward() solveSideBit() portalToggler() solveSideBit() portalToggler() moveForward()
}
func TSolver() { portalToggler() for i in 1 ... 7 {
if isOnGem { collectGem() gemCounter += 1 } if isBlockedLeft && isBlocked && isBlockedRight{ turnLeft() turnLeft() moveForward() } else if isBlocked && !isBlockedLeft{ turnLeft() moveForward() } else if isBlockedRight && isBlockedLeft { moveForward() } else if isBlockedRight{ turnLeft() moveForward() } else { moveForward() } }
}
while gemCounter < totalGems { sidesolver() TSolver() }
When the gem is on starting point your code ignore it > u need to fix this
Here is mine
let totalGems = randomNumberOfGems
var collectedGems = 0
func goAndTake() {
moveForward()
if isOnGem {
collectGem()
collectedGems += 1
}
}
func turnAround() {
turnRight()
turnRight()
}
func turnOff() {
bluePortal.isActive = false
pinkPortal.isActive = false
turnAround()
}
func turnOn() {
bluePortal.isActive = true
pinkPortal.isActive = true
turnAround()
}
while totalGems != collectedGems {
while !isBlocked {
goAndTake()
}
turnOff()
while !isBlocked {
goAndTake()
}
turnOn()
}
I realize this neglects the same issues @beResonant says above, it wasn’t necessary for the activity so I’m fine leaving it.
I believe I could edit a check after “isBlocked” before the turn that would check for “isBlockedLeft || isBlockedRight” and search but didn’t feel it necessary
Additionally the “isOnGem” in the. “IsBlocked” section was only added to make byte collect before turning, seems more important to collect than to turn around.
var gemCounter = 0
let totalGems = randomNumberOfGems
while gemCounter != totalGems {
if isOnGem {
collectGem()
gemCounter += 1
}
moveForward()
if isBlocked {
if isOnGem {
collectGem()
gemCounter += 1
}
turnRight()
turnRight()
if bluePortal.isActive == true {
bluePortal.isActive = false
} else {
bluePortal.isActive = true
}
}
if bluePortal.isActive == true {
pinkPortal.isActive = true
} else {
pinkPortal.isActive = false
}
}
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.
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
}
}
}
How would your character navigate the ends of the middle section? The playground does not indicate if gems will appear here, and you don't know they won't appear here without running the code several times beforehand.
