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