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:

  1. The Englishman lives in the house with red walls.
  2. The Swede keeps dogs.
  3. The Dane drinks tea.
  4. The house with green walls is just to the left of the house with white walls.
  5. The owner of the house with green walls drinks coffee.
  6. The man who smokes Pall Mall keeps birds.
  7. The owner of the house with yellow walls smokes Dunhill.
  8. The man in the center house drinks milk.
  9. The Norwegian lives in the first house.
  10. The Blend smoker has a neighbor who keeps cats.
  11. The man who smokes Blue Masters drinks beer.
  12. The man who keeps horses lives next to the Dunhill smoker.
  13. The German smokes Prince.
  14. The Norwegian lives next to the house with blue walls.
  15. 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.

This post is filed under categories: Math, Problems