Monday, February 06, 2012

Analytical Reasoning With Prolog

Last year, I solved the following analytical reasoning problem by hand. Post learning Prolog, I think this is a good problem to solve using a computer! I had a interesting experience doing so.

Problem:

Facts:
1: There are 5 villas in 5 different colors 
2: In each villa lives a person with a different nationality. 
3: These 5 owners drink a certain beverage, smoke a certain brand of cigar and keep a certain pet. 
4: No owner has the same pet, smoke the same brand of cigar or drink the same drink.
Hints:
1: The British lives in a red villa. 
2: The Swede keeps dogs as pets 
3: The Dane drinks tea 
4: The green villa is on the left of the white villa (it also means they are next door to each other) 
5: The green villa owner drinks coffee 
6: The person who smokes Pall Mall rears birds 
7: The owner of the yellow villa smokes Dunhill 
8: The man living in the villa right in the center drinks milk 
9: The Norwegian lives in the first villa 
10: The man who smokes Blend lives next to the one who keeps cats 
11: The man who keeps horses lives next to the man who smokes Dunhill 
12: The owner who smokes Blue Master drinks beer 
13: The German smokes Prince 
14: The Norwegian lives next to the blue villa 
15: The man who smokes Blend has a neighbor who drinks water.

The question is: who keeps the fish?

Prolog Solution:

The following is what i could come up with after serval iterations through code.


Answer to Puzzle:

Following is the output of the prolog program.

1,norwegian,yellow,water,dunhill,cat
2,dane,blue,tea,blend,horse
3,british,red,milk,pallmall,birds
4,german,green,coffee,prince,fish
5,swede,white,beer,bluemaster,dog

Hence the answer is German keeps the fish.