Part Of: Logic sequence
Content Summary: 1000 words, 10 min read.
Today, we look at the Zebra Puzzle (aka Einstein Puzzle). According to legend, Albert Einstein invented this as a child, and claimed that 98% of the human population cannot solve it.
Let’s see if we are in the 2%.
Five men of different nationalities and with different jobs live in consecutive houses on a street. These houses are painted different colors. The men have different pets and have different favorite drinks. The following rules are provided:
- The English man lives in a red house
- The Spaniard owns a dog
- The Japanese man is a painter
- The Italian drinks tea
- The Norwegian lives in the first house on the left
- The green house immediately to the right of the white one
- The photographer breeds snails
- The diplomat lives in the yellow house
- Milk is drunk in the middle house
- The owner of the green house drinks coffee
- The Norwegian’s house is next to the blue one
- The violinist drinks orange juice
- The fox is in a house that is next to that of the physician
- The horse is in a house next to that of the diplomat
Who owns a zebra? And whose favorite drink is mineral water?
To answer this problem, we must learn 5 house-nation-color-drink-pet-job combinations. A solution might look like this:
- Yellow far-left house has Norwegian diplomat who drinks water and owns a fox
- White left house has Italian photographer who drinks tea and owns a zebra.
- Red middle house has English photographer who drinks milk and owns snails.
- Green right house has Spanish physician who drinks OJ and owns a dog
- Blue far-right house has Japanese painter who drinks coffee and owns a horse.
But this solution is incorrect: it violates Rule 6: “The green house immediately to the right of the white one.”
How do we find a solution that doesn’t violate any of our constraints? Does one even exist? Or is this set of constraints not satisfiable?
Formalizing Logical Structure
Words are distracting. Let’s use symbols instead.
With this code, we can write the above solution as a matrix.
We can also formalize our constraints.
These constraints are ugly. Let’s write them in matrix form instead!
Constraint Satisfaction as a Jigsaw Puzzle
We can use the above constraints to visually check satisfiability. Whereas before you had to parse the meaning of Rule 6 verbally, now you can just inspect whether there is a visual match between rule and solution.
One way to determine satisfiability is to perform these checks until you find a viable solution. But this is computationally expensive: there are 25 billion solutions. Instead of inspecting every possible solutions, why don’t we generate one solution?
How? Since our Rules are used for solution-checking, why can’t we use them for solution-building?
On this view, solution building takes on the flavor of a jigsaw puzzles. Each constraint is a puzzle piece, from these ingredients we construct the solution.
Unfortunately, there is more than one way to solve a 5×5 jigsaw puzzle. Let me show you one way to solve this one. We will be use choice minimization to simplify our lives: try to play the move with the fewest degrees of freedom.
Solution: Path A
Rule 5 and 9 relate to the houses, they are easy to apply.
After these, the Rule 11 puzzle piece fits unambiguously.
Let’s apply Rule 6 next. That jigsaw piece can fit in two locations, the M+R columns, or the R + FR columns. Gotta choose one: let’s select the former. After that move, Rule 10 fits unambiguously.
The FR column is the only place that has an unclaimed nation and color: Rule 1 must go there. similarly, the FL column is the only available spot for Rule 8.
Here we can apply Rule 14 (the original clue’s wording “The horse is in a house next to that of the diplomat” means that the puzzle piece can be flipped horizontally).
After that, only column L can accommodate Rule 4. Then FR must accept Rule 12.
Disaster! Consider Clue 2, 3, and 7. These rules are mutually exclusive (they have at least one row in common with one another), and have overlapping domains (they all cannot fit in FL, but must fit in either M or R).
This is the pigeonhole principle: just as three pigeons cannot fit into two holes, there is no way to reach a solution.
Does that mean the puzzle is unsolvable? No, it means we explore other choices.
Solution: Path B
Let’s return to the other possible placement of Rule 6. Instead of putting it in M+R columns, we’ll put it in R+FR. Then, Rules 10, 1, 8, and 14 follow inevitably (each has precisely one choice).
Here we face another choice: do we put puzzle piece 4 in the left or right house? Let’s choose the right house. Then, Rule 12 and Rule 3 follow logically.
Alas! Another disaster. Rule 2 doesn’t fit. 😦
Solution: Path C
Retrace our steps! The last choice we made was Place(4, R). What if we place it in the left house instead?
To our delight, we now see that Path 2b is the only correct logical journey through our puzzle. The concluding steps are given below, and the desired quantities are shown in the “missing” tiles.
Recall the original questions:
Who owns a zebra (P5)? Whose favorite drink is mineral water (D5)?
Our symbol table can translate our answer:
The Japanese man (N3) owns the zebra, and the Norwegian (N5) drinks mineral water
For the solution above, it is helpful to review our search history. Remarkably, we only faced two choices in our solution. When one branch failed, we turned out attention to other branches. This is known as recursion, and will be the subject of another blog post.
This kind of problem is called propositional satisfiability (SAT), or constraint programming (CP), although these two disciplines differ in subtle ways.