Kuro5hin.org: technology and culture, from the trenches
create account | help/FAQ | contact | links | search | IRC | site news
[ Everything | Diaries | Technology | Science | Culture | Politics | Media | News | Internet | Op-Ed | Fiction | Meta | MLP ]
We need your support: buy an ad | premium membership

[P]
Einstein's puzzle -- can you solve it?

By tmoertel in tmoertel's Diary
Wed Jan 17, 2001 at 01:19:33 AM EST
Tags: (all tags)

The following puzzle was rumored to have been created by Einstein, who claimed that 98% of the world could not solve it. As the story goes, he created the puzzle to occupy his students while he pursued his theories. While you can solve the puzzle by hand (and you might wish to do so as an entertaining party exercise), I think it makes a nifty programming challenge.


ADVERTISEMENT
Sponsor: rusty
This space intentionally left blank
...because it's waiting for your ad. So why are you still reading this? Come on, get going. Read the story, and then get an ad. Alright stop it. I'm not going to say anything else. Now you're just being silly. STOP LOOKING AT ME! I'm done!
comments (24)
active | buy ad
ADVERTISEMENT
Einstein's Puzzle

Given that:

  1. There are 5 houses in 5 different colors.
  2. Each house is owned by a man of a different nationality.
  3. The 5 owners drink a certain type of beverage, smoke a certain brand of cigar, and keep a certain pet.
  4. No owners have the same pet, smoke the same brand of cigar, or drink the same beverage.

The question is: Who owns the fish?

Clues:

  1. The Brit lives in the red house.
  2. The Swede keeps dogs as pets.
  3. The Dane drinks tea.
  4. The green house is on the left of the white house.
  5. The green house's owner drinks coffee.
  6. The person who smokes Pall Mall rears birds.
  7. The owner of the yellow house smokes Dunhill.
  8. The man living in the center house drinks milk.
  9. The Norwegian lives in the first house.
  10. The man who smokes Blends lives next to the one who keeps cats.
  11. The man who keeps the horse lives next to the man who smokes Dunhill.
  12. The owner who smokes Bluemasters drinks beer.
  13. The German smokes Prince.
  14. The Norwegian lives next to the blue house.
  15. The man who smokes Blends has a neighbor who drinks water.

Can you write a program to find the solution(s) to the puzzle?

The rules:

  • Use any programming language that you want.
  • Your program must be correct, i.e., produce the correct answer(s).
  • After that, the following are desirable characteristics:
    • clear, concise code
    • reasonable execution time
    • short, sweet documentation

Sponsors

Voxel dot net
o Managed Hosting
o VoxCAST Content Delivery
o Raw Infrastructure

Login

Related Links
o tmoertel's Diary


Display: Sort:
Einstein's puzzle -- can you solve it? | 5 comments (5 topical, editorial, 0 hidden)
Already done. (none / 0) (#1)
by i on Wed Jan 17, 2001 at 06:15:08 AM EST

Solving a logic puzzle with Prolog.

and we have a contradicton according to our assumptions and the factor theorem

Done it before in Prolog (none / 0) (#4)
by Logan on Sun Apr 14, 2002 at 07:21:33 PM EST

This was a prolog programming assignment two years ago when I was taking comparative languages. It was fun to implement; it's not the most straight-forward puzzle you can do in prolog (I recall having to do some things just right to prevent the search space from becoming too unmanageably huge), but not too difficult. Prolog is made for this sort of thing.

Unfortunately, I don't have the code lying around still (unless it's on one of my computers at home; I'll have to check).

Logan

A prolog solution (none / 0) (#5)
by teemu111 on Wed May 29, 2002 at 03:32:46 AM EST

/*I followed instructions at http://www.cs.yorku.ca/course_archive/1998-99/F/3401/lectures/340198-10-26HTML/ */
/*Part one: Preparations makeHouses/2 generates a list containing N houses, having attributes for the color of the house, nationality of the habitant, his pet,drink ang cigarettes.
*/
makeHouses(0,[]).
makeHouses(N,[house(Color,Nat,Pet,Drink,Cig)|List]):-N > 0, N1 is N -1,makeHouses(N1,List).
%on/2 is declared as infix operator and is true if X is on the list Xs.
:-op(100,xfy,on).
X on [X|Xs].
X on [_|Xs]:-X on Xs.
%sublist/2 for the nextTo and leftTo sublist([S1,S2],[S1,S2|L]).
sublist(S,[_|T]):-sublist(S,T).
nextTo(H1,H2,L):-sublist([H1,H2],L).
nextTo(H1,H2,L):-sublist([H2,H1],L).
leftTo(G,W,L):-sublist([G,W],L).
/*Part two: Solving the puzzle*/
/*solve1/0 shows the answer*/
solve:-solve(FishOwner),nl,write('The '),write(FishOwner),write(' owns the fish.'),nl.
/*solve/1 FishOwner is the nationality of fishowner*/
solve(FishOwner):-makeHouses(5,List),/*generates a list, whinch is constrained by following clues*/
/*The Norwegian lives in the first house.
The Norwegian lives next to the blue house.
The man living in the center house drinks milk.
(These are put in the first place,
so there's not so much need to rearrange the order of houses in latter constraints)*/
List=[house(_,norwegian,_,_,_),house(blue,_,_,_,_),house(_,_,_,milk,_),_,_],
/*The man who smokes Blends lives next to the one who keeps cats:*/
nextTo(house(_,_,_,_,blends),
house(_,_,cats,_,_),
List),
/*The man who keeps the horse lives next to the man who smokes Dunhill*/
nextTo(house(_,_,horse,_,_), house(_,_,_,_,dunhill),List),
/*The green house's owner drinks coffee and the green house is on the left of the white house:*/
leftTo(house(green,_,_,coffee,_),house(white,_,_,_,_),List),
/*The man who smokes Blends has a neighbor who drinks water:*/
nextTo(house(_,_,_,_,blends),house(_,_,_,water,_),List),
/*The Brit lives in the red house:*/
house(red,brit,_,_,_) on List,
/*The Swede keeps dogs as pets:*/
house(_,swede,dogs,_,_) on List,
/*The Dane drinks tea.
*/
house(_,dane,_,tea,_) on List,
/*The person who smokes Pall Mall rears birds:*/
house(_,_,birds,_,pallmall) on List,
/*The owner of the yellow house smokes Dunhill:*/
house(yellow,_,_,_,dunhill) on List,
/*The owner who smokes Bluemasters drinks beer:*/
house(_,_,_,beer,bluemasters) on List,
/*The German smokes Prince:*/
house(_,german,_,_,prince) on List,
/*"FishOwner" is bound to the nationality of the fish owner*/
house(_,FishOwner,fish,_,_) on List.


Einstein's puzzle -- can you solve it? | 5 comments (5 topical, 0 editorial, 0 hidden)
Display: Sort:

kuro5hin.org

[XML]
All trademarks and copyrights on this page are owned by their respective companies. The Rest © 2000 - Present Kuro5hin.org Inc.
See our legalese page for copyright policies. Please also read our Privacy Policy.
Kuro5hin.org is powered by Free Software, including Apache, Perl, and Linux, The Scoop Engine that runs this site is freely available, under the terms of the GPL.
Need some help? Email help@kuro5hin.org.
My heart's the long stairs.

Powered by Scoop create account | help/FAQ | mission | links | search | IRC | YOU choose the stories!