Swift Playground Part 5: Logical Operators

In this section we learn about logical operators. There are 3 to learn. AND, NOT and OR. The symbols we use are double ampersand (&&) for AND, exclamation mark (!) for NOT and the double pipe symbol (||) for OR.

Using the NOT operator

In this challenge, the key is to look for a trigger to turn left down the stairs. If you run the puzzle a few times you’ll see the position of the stairs randomly change but the tile prior to the stairs does “NOT” have a gem. This is a clue. You can then use a for loop as well.

Using the NOT operator - Simple solution
for i in 1 ... 4 {
    moveForward()
    if !isOnGem {
        // go downstairs and collect gem
        turnLeft()
        moveForward()
        moveForward()
        collectGem()
        
        // turn around
        turnLeft()
        turnLeft()
        
        // go back upstairs
        moveForward()
        moveForward()
        turnLeft()
    } else {
        collectGem()
    }
}
Using the NOT operator - Solution with functions
func collectGemDownstairs() {
    turnLeft()
    moveForward()
    moveForward()
    collectGem()
    turnAround()
    moveForward()
    moveForward()
    turnLeft()
}

func turnAround() {
    turnLeft()
    turnLeft()
}
for i in 1 ... 4 {
    moveForward()
    if !isOnGem {
        collectGemDownstairs()
    } else {
        collectGem()
    }
}

Spiral of NOT

This challenge is again dynamic in that you don’t know how far you have to walk but if you look closely, you’ll notice that you always have to turn left and in particular, you always turn left when you are blocked from the front.

That should be all the clues that you need to get you started. Else, you could jump straight to the answers below or watch the video for an explanation.

Spiral of NOT - Solution 1
for i in 1 ... 13 {
    if !isBlocked {
        moveForward()
    } else {
        turnLeft()
        moveForward()
    }
}
toggleSwitch() 
Spiral of NOT - Better Solution
for i in 1 ... 13 {
    if isBlocked {
        turnLeft()
    } 
    moveForward()
}
toggleSwitch()


Checking this AND that

Here the pattern is that every time you are blocked on the left, you have a set of stairs to climb except on the second green tile. On the second green tile, you are blocked on the left but no stairs to climb. However, notice that the switch is already toggled on so you can ignore it.

The secret is to check if you are on a gem AND blocked on the left. Then put the rest into a for loop.

If you are up for a challenge, take the sample solution below, and make it better by using functions.

Checking this AND that - Solution
for i in 1 ... 7 {
    moveForward()
    if isBlockedLeft && isOnGem {
        collectGem()
        turnRight()
        moveForward()
        moveForward()
        toggleSwitch()
        turnLeft()
        turnLeft()
        moveForward()
        moveForward()
        turnRight()
    } else if isOnGem {
        collectGem()
    }
}
Checking this AND that - Solution using functions
func turnAround() {
    turnLeft()
    turnLeft()
}

func toggleSwitchUpTheStairs() {
    collectGem()
    turnRight()
    moveForward()
    moveForward()
    toggleSwitch()
    turnAround()
    moveForward()
    moveForward()
    turnRight()
}
for i in 1 ... 7 {
    moveForward()
    if isBlockedLeft && isOnGem {
        toggleSwitchUpTheStairs()
    } else if isOnGem {
        collectGem()
    }
}

Checking this OR that

This challenge looks tricky at first sight but upon closer inspection, you’ll notice that you always have to turn right and when you do, there is always either a wall in front of you OR a wall on your left.

Checking this OR that - Solution 1
for i in 1 ... 12 {
    if isBlocked || isBlockedLeft {
        turnRight()
        moveForward()
    } else {
        moveForward()
    }
}
collectGem() 
Checking this OR that - A better solution
for i in 1 ... 12 {
    if isBlocked || isBlockedLeft {
        turnRight()
    } 
    moveForward()
}
collectGem()
                

Logical labyrinth

The pattern in this challenge is whenever there is a gem, you need to turn right and collect the other gem down the stairs.

You then have a few options with the other condition. You could use the is blocked on the right or if the switch is toggled off pattern to trigger a left turn. That should be enough to get you going. Otherwise, check out the answer below.

Logical Labyrinth - Simple solution
for i in 1 ... 8 {
    moveForward()
    if isOnGem && isOnClosedSwitch {
        collectGem()
        toggleSwitch()
        turnRight()
        moveForward()
        moveForward()
        collectGem()
        turnLeft()
        turnLeft()
        moveForward()
        moveForward()
        turnRight()
    } else if isOnClosedSwitch {
        toggleSwitch()
        turnLeft()
    } else if isOnGem {
        collectGem()
    }
}
Logical Labyrinth - Solution with functions
func turnAround() {
    turnLeft()
    turnLeft()
}

func collectGemDownStairs() {
    turnRight()
    moveForward()
    moveForward()
    collectGem()
    turnAround()
    moveForward()
    moveForward()
    turnRight()
}
for i in 1 ... 8 {
    moveForward()
    if isOnGem && isOnClosedSwitch {
        collectGem()
        toggleSwitch()
        collectGemDownStairs()
    } else if isOnClosedSwitch {
        toggleSwitch()
        turnLeft()
    } else if isOnGem {
        collectGem()
    }
}

See all solutions in the video below

Leave a Reply

Your email address will not be published. Required fields are marked *