The Problem of 100 Doors

Plenty of interesting and fun programming puzzles are available to test your skills. Some of these puzzles come from the realm of mathematics or logic. What those propellerheads do with the solutions is up to them, but often you can code such problems to help you learn more about programming. One such logic problem is called 100 Doors.

This post isn’t about the 100 Doors game/app available for mobile devices. I wasn’t aware of that app when I originally wrote the post.

The 100 Doors programming puzzle works like this: You have a series of 100 doors. You start with the first door and work your way to the last door. At each stop, if a door is closed you open it or if a door is open you close it. Yes, this process is similar to flipping bits, but with doors instead of bits.

For your first pass, all the doors are closed, so you open them. The second pass, you start at door 2 and skip every second door, opening and closing doors along the way. The third pass, you start at door 3 and skip every third door, opening and closing. This process continues up until door 100.

The question is which doors are open or closed after the 100th pass?

To answer the question, you can construct a tiny diorama with 100 doors and work through all of them, open-close, open-close. Or you can write a program.

Like many programming puzzles, the purpose of the 100 doors problem is to see how you solve it. Several approaches are possible and, potentially, each programmer codes a unique solution.

When I tackled the problem, I immediately thought of the several required programming elements: an array for the doors, a routine to open or close the doors, and some loops to process the doors.

First comes the array:

    int door[DOORS] = { 0 };

The above statement creates a int array door[]. The array has DOORS elements, which is defined as 100. The single value in the braces initializes all elements of the array to zero, i.e., all the doors are closed.

Second, you need a routine to open or close the doors. If a door element is 1 it must be flipped to 0 and vice-versa. You have several choices.

The most readable choice is to use an if-else structure:

    if(door[thisdoor])  /* door is open */
        door[thisdoor] = 0;
    else
        door[thisdoor] = 1;

In the above code snippet, thisdoor is a variable representing some element in the door[] array, most likely it’s a variable from a loop. If the door is open, the element is 1 or TRUE. The if statement resets the value to zero or “closed.” Otherwise, the value is zero, so it’s reset to 1 or “open.”

You could also use the ternary operator to evaluate and set open or closed doors:

    door[thisdoor] = door[thisdoor] ? 0 : 1;

This method is not as readable as the if-else structure, though the grizzled C coding nerds probably would enjoy the format.

Another option is to use the logical ! (not) operator to flip the bits in a value:

    door[thisdoor] = !door[thisdoor];

This statement isn’t as readable as the if-else structure, but it’s brief and not as cryptic as the ternary operator.

Finally, the code needs a loop. Actually, it needs two loops: One to go from door 1 to door 100 and a second loop that skips through the doors in increments of 1, 2, 3, and so on. I’ll review those options in next week’s Lesson, and present my solution to the puzzle.

Leave a Reply