Skip to content

Instantly share code, notes, and snippets.

@imbradmiller
Created September 26, 2016 13:01
Show Gist options
  • Save imbradmiller/9b7944323e953b0406f9914e324bc67b to your computer and use it in GitHub Desktop.
Save imbradmiller/9b7944323e953b0406f9914e324bc67b to your computer and use it in GitHub Desktop.
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()
}
}
@KolemanIreland
Copy link

I ran this code and it didn’t solve the puzzle.
de230669-2206-4d5d-9c1c-de90d93be032

Commands ended there.

@benjaminpadula
Copy link

benjaminpadula commented Oct 12, 2020

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()
    }
} 

@aenakin
Copy link

aenakin commented Feb 5, 2022

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