BYU logo Computer Science

Bit Puzzles

These puzzles are designed to help you practice the ideas we review in class.

Some of them are easy. Some of them are more difficult. Don’t get discouraged if it takes you a few tries to get something right. Take it slow and steady. Draw pictures.

Setup

To begin, use PyCharm to create a project called puzzles. You should do all your work there.

Next, download bit-puzzles.zip. This is a zip file, so you will need to extract its contents. You should put the folder called worlds inside of your puzzles folder.

When you are done, your puzzles folder should look like this:

puzzles folder

Borders

This problem starts with a blank world:

borders start

You should paint a blue border around the world:

borders finish

from byubit import Bit
bit = Bit.load("worlds/borders-start.txt")

# Fill me in!

bit.compare(Bit.load("worlds/borders-finish.txt"))

Fix the Tree

This was an example we went through in class. Can you solve it yourself? Here is the start world:

fix tree start

and the ending:

fix tree finish

from byubit import Bit
bit = Bit.load("worlds/fix-tree-start.txt")

# Fill me in!

bit.compare(Bit.load("worlds/fix-tree-finish.txt"))

Reverse Coyote

The coyote has run off the cliff and wants to get back. Paint the first square green and then the next squares blue until he is back on the cliff. The starting world looks like this:

reverse coyote start

The ending world should look like this:

reverse coyote finish

from byubit import Bit
bit = Bit.load("worlds/reverse-coyote-start.txt")

# Fill me in!

bit.compare(Bit.load("worlds/reverse-coyote-finish.txt"))

Coyote Fail

The coyote runs off the cliff, hits a wall, and falls into the water. Paint squares green as he moves. The starting world looks like this:

coyote fail start

The ending world should look like this:

coyote fail finish

from byubit import Bit
bit = Bit.load("worlds/coyote-fail-start.txt")

# Fill me in!

bit.compare(Bit.load("worlds/coyote-fail-finish.txt"))

Simple Maze

Help Bit navigate out of a maze.

The code you write should allow Bit to navigate any maze of similar design:

  • There is only one direction Bit can go at any time
  • Hint: go until blocked, then check which direction is clear and go that way.

The starting world looks like this:

simple maze start

The ending world should look like this:

simple maze finish

from byubit import Bit
bit = Bit.load("worlds/simple-maze-start.txt")

# Fill me in!

bit.compare(Bit.load("worlds/simple-maze-finish.txt"))

Your solution should also work for a second maze, which looks like this:

simple maze2 start

The ending world should look like this:

simple maze finish

from byubit import Bit
bit = Bit.load("worlds/simple-maze2-start.txt")

# Fill me in!

bit.compare(Bit.load("worlds/simple-maze2-finish.txt"))

Flying

For this puzzle, follow these rules:

  • Move Bit forward while clear.
  • When Bit finds red, turn left.
  • When Bit finds blue, turn right.
  • Paint the trail green.

Here is the starting world:

flying start

The ending world should look like this:

flying finish

from byubit import Bit
bit = Bit.load("worlds/flying-start.txt")

# Fill me in!

bit.compare(Bit.load("worlds/flying-finish.txt"))

Falling Water

A stream flows over the edge of a cliff and falls to the pool below. As it falls, only every other square is blue.

Here is the starting world:

falling water start

The ending world should look like this:

falling water finish

from byubit import Bit
bit = Bit.load("worlds/falling-water-start.txt")

# Fill me in!

bit.compare(Bit.load("worlds/falling-water-finish.txt"))

Colorful Smiles

Draw smiles that correspond to the color of block encountered. The starting world looks like this:

smiles start

You can write your code in colorful_smiles.py and use this to load the starting world:

from byubit import Bit
bit = Bit.load("worlds/colorful-smiles-start.txt")

The ending world should look like this:

smiles finish

If your function to draw the smiles is draw_smiles(), you can test your solution with the following code:

bit.compare(Bit.load("worlds/colorful-smiles-finish.txt"))

Tip

In the previous activity, you probably wrote a function that looked something like this:

def draw_smile(bit):
    ...
    bit.paint("blue")
    ...

Now, pass the color as an argument to the function:

def draw_smile(bit, color):
    ...
    bit.paint(color)
    ...

Now, as Bit moves forward, it can find the color of the current square, and use it to decide what color the smile should be.

Fill in the blanks

Fill in every square that isn’t blank.

The starting world looks like this:

fill in the blanks start

The ending world should look like this:

fill in the blanks finish

Hints:

  • There is a single path with straight channels off the sides.
  • As you move, check if the left side is clear. If it is, fill in the channel on that side. Otherwise, check if the right side is clear, and if it is, fill in the channel on that side.
  • After you fill in a channel, come back to where you were.
  • You should write a function to fill in a channel, and another function to come back.
from byubit import Bit
bit = Bit.load("worlds/fill-in-the-blanks-start.txt")

# Fill me in!

bit.compare(Bit.load("worlds/fill-in-the-blanks-finish.txt"))

Changing colors

This task requires variables, which won’t be discussed until later in the course.

Move Bit until blocked. Paint each square. Change the paint color when you cross a square that isn’t empty.

The starting world looks like this:

changing colors start

The ending world should look like this:

changing colors start

You can get the current color and store it in a variable like this:

color = bit.get_color()

You can use this color when painting.

from byubit import Bit
bit = Bit.load("worlds/changing-colors-start.txt")

# Fill me in!

bit.compare(Bit.load("worlds/changing-colors-finish.txt"))