There are several logical grid puzzles you can find on the internet, but undoubtedly, one of the most popular known is the Einstein's riddle. Although it's attributed to Einstein, there's no evidence to back up that claim. It's rumoured that only 2% of the world can solve it, but it's really not so hard, all one needs is logic and deduction. In this article we are going to present a method based entirely on matrix operations to solve this riddle and any other similar.

## The riddle

There are five houses in five different colours in a row. In each house lives a person with a different nationality. The five owners drink a certain type of beverage, smoke a certain brand of cigar and keep a certain pet. No owners have the same pet, smoke the same brand of cigar, or drink the same beverage. The question is: Who owns the fish?

**Hints:**

- The British man lives in the red house.
- The Swedish man has a dog for a pet.
- The Danish man drinks tea.
- The Norwegian lives in the first house.
- The German smokes Prince.
- The green house is to the left of the white house.
- The owner of the green house drinks coffee.
- The person that smokes Pall Mall has a bird.
- The owner of the yellow house smokes Dunhill.
- The person that lives in the middle house drinks milk.
- The person that smokes Blend, lives next to the one that has a cat.
- The person that has a horse lives next to the one that smokes Dunhill.
- The one that smokes Bluemaster drinks beer.
- The person that smokes Blend, has a neighbour that drinks water.
- The Norwegian lives next to a blue house.

## Matrix formulation

This problem can be formulated in terms of matrices, where the goal is to complete a matrix using the hints provided.

For this particular problem the start matrix is the following:

Position | Nationality | Color | Drink | Smoke | Pet |
---|---|---|---|---|---|

1 | |||||

2 | |||||

3 | |||||

4 | |||||

5 |

Where the position of the houses can be fixed.

Every hint can be expressed by means of a matrix, where the amount of columns is equal to the amount of columns of the start matrix. Use blanks for the unknown cells. For example, hints 1 and 2 can be expressed as:

**1**

Position | Nationality | Color | Drink | Smoke | Pet |
---|---|---|---|---|---|

English | red |

**2**

Position | Nationality | Color | Drink | Smoke | Pet |
---|---|---|---|---|---|

Swedish | dog |

Hint 6 involves two rows:

**6**

Position | Nationality | Color | Drink | Smoke | Pet |
---|---|---|---|---|---|

green | |||||

white |

And hint 11 is more complex, because it involves two options. As the hint states the person that smokes Blend, lives next to the one that has a cat, it's not clear if the person who smokes Blend lives before or after the one who keeps cats, so we need to consider both options.

**11**

Position | Nationality | Color | Drink | Smoke | Pet |
---|---|---|---|---|---|

cat | |||||

Blend |

Position | Nationality | Color | Drink | Smoke | Pet |
---|---|---|---|---|---|

Blend | |||||

cat |

## Matrix operations

As we are dealing with string matrices, there is no sense talking about numerical operations, but some kind of operations that allow working with any kind of value. The solution we propose needs to define three operations, where the operands are matrices with equal amount of columns, and the result is, in two cases a Boolean value (true or false), and in the third case it's a matrix with equal amount of columns as the operands.

1. **Has a common value**: This is a simple operator that checks if two matrices have a common value (not blank) in the same column position no matter the row.

2. **Can be merged**: This is a Boolean operator that returns true if two matrices can be merged using the common value found by means of the previous operator as the point of merger. This operator should return false if there are two overlapped non-blank cells with different values, or the merging process requires adding a new row when there is no sense the matrix can grow.

3. **Merge**: This operator just merges two matrices using a common cell as the point of merger, adding new rows if it's necessary.

In the next part, we'll be talking about the algorithm to solve the riddle using these three matrix operations, and a JavaScript implementation to solve this riddle and any other grid riddle similar to this one.

## You might be interested in

**Version:**1.26

**Price:**$22.95 USD (Full version - Buy Now)

**Downloadable version:**Demo version - Limited functionalities

**Operating system:**Windows XP / Vista / 7 / 8

**Size:**3.2 MB (3270 KB)

## Still no comments