Einstein’s Riddle
originally published on February 4, 2024
This is a famous riddle:
There are five houses lined up next to each other along a street. Each house is a different color, and each homeowner is of a different nationality, drinks a different beverage, smokes a different brand of cigar, and owns a different pet.
If these variables can never repeat from house to house, which homeowner has a pet fish?
You should be able to figure out the answer based on these 15 clues:
- The Englishman lives in the house with red walls.
- The Swede keeps dogs.
- The Dane drinks tea.
- The house with green walls is just to the left of the house with white walls.
- The owner of the house with green walls drinks coffee.
- The man who smokes Pall Mall keeps birds.
- The owner of the house with yellow walls smokes Dunhill.
- The man in the center house drinks milk.
- The Norwegian lives in the first house.
- The Blend smoker has a neighbor who keeps cats.
- The man who smokes Blue Masters drinks beer.
- The man who keeps horses lives next to the Dunhill smoker.
- The German smokes Prince.
- The Norwegian lives next to the house with blue walls.
- The Blend smoker has a neighbor who drinks water.
I worked on this problem and came up with a solution that agrees with other solutions on the Internet.
But, my approach is not the typical one used for logic problems like this. Instead, as a computer programmer, I wrote a searching algorithm that tests all possibilities in the problem space and returns all correct solutions (and it turns out that there’s just one solution).
The code is written in JavaScript. Initially, I created a quick-and-dirty implementation running at home on node.js and put that on JSFiddle where all output is displayed in the console. Later, I added it to my collection of solved problems.