# Einstein’s riddle

The following puzzle is said to be invented by Einstein. Supposedly, he also claimed that only 2% of the world’s population would be smart enough to solve it.

There are 5 houses in 5 different colors in a row. In each house lives a person with a different nationality. These 5 owners drink a certain drink, 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 drink.

The question is: WHO OWNS THE FISH?

HINTS:

• the Brit lives in the red house
• the Swede keeps dogs as pets
• the Dane drinks tea
• the green house is on the immediate left of the white house
• the green house owner drinks coffee
• the person who smokes Pall Mall rears birds
• the owner of the yellow house smokes Dunhill
• the man living in the house right in the center drinks milk
• the Norwegian lives in the first house
• the man who smokes blends lives next to the one who keeps cats
• the man who keeps horses lives next to the one who smokes Dunhill
• the owner who smokes Bluemaster drinks beer
• the German smokes prince
• the Norwegian lives next to the blue house
• the man who smokes blends has a neighbor who drinks water

Working out the solution with nothing more that a pen and some paper is certainly doable by, I suspect hope, a larger percentage of people than the 2 % mentioned above. But as an example of how to solve these kinds of logic puzzles using Prolog, I wrote this code.

# Sudoku solver

It’s pretty straightforward to make a Sudoku solver in Prolog especially when applying CLP (Constraint Logic Programming).

Here is how to use my program:

`:- solve_sudoku.`

Then you can enter the known numbers one by one.

```+-+-+-+-+-+-+
|9 8| 5 |  7|
|72 |1```

When complete, the program determines and prints the solution.

```+-+-+-+-+-+-+
|938|254|617|
|726|193|854|
|514|876|329|
+-+-+-+-+-+-+
|692|387|145|
|187|465|293|
|453|921|768|
+-+-+-+-+-+-+
|345|712|986|
|871|649|532|
|269|538|471|
+-+-+-+-+-+-+```

Typing

`:- sudoku(L), write_sol(L).`

Gives

```+-+-+-+-+-+-+
|123|456|789|
|456|789|123|
|789|123|456|
+-+-+-+-+-+-+
|214|365|897|
|365|897|214|
|897|214|365|
+-+-+-+-+-+-+
|531|642|978|
|642|978|531|
|978|531|642|
+-+-+-+-+-+-+```

By pressing ; over and over again, you could enumerate all 6,670,903,752,021,072,936,960 possible Sudoku solution grids, but this might take a while..

It shouldn’t be too hard to extend this program to actually create new puzzles. If anyone does, let me know.

The prolog environment I used here is SWI-Prolog.

# A brainf*ck interpreter in Prolog

The language brainfuck is extremely minimalistic, consists of only eight commands and is Turing complete. Writing an interpreter for it in Prolog is a breeze.

You can download my interpreter here. I’ve included some brainfuck programs I found at ‘The brainfuck archive’.

To start a brainfuck program from Prolog do the following:

`:- run_bf_program('hellobf.bf').`

It’s not the fastest interpreter around, but it’s a nice example of how to build one in Prolog.