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?
- 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.
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:
Then you can enter the known numbers one by one.
|9 8| 5 | 7|
When complete, the program determines and prints the solution.
:- sudoku(L), write_sol(L).
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.
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:
It’s not the fastest interpreter around, but it’s a nice example of how to build one in Prolog.