Skip to content

Instantly share code, notes, and snippets.

@barnybug
Created May 5, 2015 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barnybug/68bd8a77f9132316b64c to your computer and use it in GitHub Desktop.
Save barnybug/68bd8a77f9132316b64c to your computer and use it in GitHub Desktop.
% Prolog solution to:
% http://www.theguardian.com/science/alexs-adventures-in-numberland/2015/may/04/einsteins-election-riddle-are-you-in-the-two-per-cent-that-can-solve-it
left(X, Y, [X | [Y | _]]).
left(X, Y, [_ | List]) :- left(X, Y, List).
neighbour(X, Y, List) :- left(X, Y, List); left(Y, X, List).
% house(Person, Pattern, Pet, Drink, Transport)
solution(Answer) :- Z = [_, _, _, _, _],
% 1. Nicola lives in the tartan house.
member(house(nicola, tartan, _, _, _), Z),
% 2. Ed has a guinea pig
member(house(ed, _, guineapig, _, _), Z),
% 3. David drinks mochaccino
member(house(david, _, _, mochaccino, _), Z),
% 4. The paisley house is on the left of the gingham house
left(house(_, paisley, _, _, _), house(_, gingham, _, _, _), Z),
% 5. The owner of the paisley house drinks flat whites.
member(house(_, paisley, _, flatwhite, _), Z),
% 6. The person who drives by car has a squirrel.
member(house(_, _, squirrel, _, car), Z),
% 7. The owner of the striped house travels by bike.
member(house(_, striped, _, _, bike), Z),
% 8. The person living in the centre house drinks double espresso.
Z = [_, _, house(_, _, _, doubleexpresso, _), _, _],
% 9. Nick lives in the first house.
Z = [house(nick, _, _, _, _), _, _, _, _],
% 10. The person who travels by train lives next to the one who has a pitbull.
neighbour(house(_, _, _, _, train), house(_, _, pitbull, _, _), Z),
% 11. The person who has a badger lives next to the person who travels by bike.
neighbour(house(_, _, badger, _, _), house(_, _, _, _, bike), Z),
% 12. The person who travels by plane drinks chai latte.
member(house(_, _, _, chailatte, plane), Z),
% 13. Nigel goes everywhere by foot.
member(house(nigel, _, _, _, foot), Z),
% 14. Nick lives next to the polka dot house.
neighbour(house(nick, _, _, _, _), house(_, polkadot, _, _, _), Z),
% 15. The person who travels by train has a neighbour who drinks decaf.
neighbour(house(_, _, _, _, train), house(_, _, _, decaf, _), Z),
% Who owns the fish?
member(house(Answer, _, fish, _, _), Z).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment