Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

prolog - Einstein Riddle with List of terms

I implemented Einstein Riddle in Prolog and I'm trying to find out who had a fish at home.
I can't find fault in this code and trace option is not helping with this problem ;)

Rules:

  1. Norwegian lives in first house
  2. The Englishman lives in a red house.
  3. The green house is located directly on the left side of the white house.
  4. Dane drink tea.
  5. Light smoker lives next to the breeders of cats.
  6. A resident of the yellow house smokes a cigar.
  7. German smokes a water-pipe.
  8. A resident of the center house drinks milk.
  9. Light smoker has a neighbor who drink the water.
  10. Smoke cigarettes without filter breeding birds.
  11. Swede bred dogs.
  12. The Norwegian lives next to the blue house.
  13. Breeder of horses lives next to the yellow house.
  14. Smoke menthol drink beer.
  15. In the green house they drink coffee.

Here is my code:

on_the_left(X, Y, N) :-
    Y is X - 1,
    + Y < 1,
    + X > N.

next_to(X, Y, N) :-
    ( Y is X + 1;
      Y is X - 1),
    + X > N,
    + Y > N,
    + X < 1,
    + Y < 1.

fish(Who) :-
    Houses = [
        house(1, _Color1, _From1, _Animal1, _Drink1, _Smoke1),
        house(2, _Color2, _From2, _Animal2, _Drink2, _Smoke2),
        house(3, _Color3, _From3, _Animal3, _Drink3, _Smoke3),
        house(4, _Color4, _From4, _Animal4, _Drink4, _Smoke4),
        house(5, _Color5, _From5, _Animal5, _Drink5, _Smoke5) ],
    N is 5,
    %-- hint 1
    member(house(1, _, norway, _, _, _), Houses),
    %-- hint 2
    member(house(_, red, england, _, _, _), Houses),
    %-- hint 3 - on_the_left
    member(house(GREEN, green, _, _, _, _), Houses),
    member(house(WHITE, white, _, _, _, _), Houses),
    on_the_left(GREEN, WHITE, N),
    %-- hint 4
    member(house(_, _, denmark, _, tea, _), Houses),
    %-- hint 5 - next_to
    member(house(LIGHT, _, _, _, _, light), Houses),
    member(house(CAT, _, _, cat, _, light), Houses),
    next_to(LIGHT, CAT, N),
    %-- hint 6
    member(house(_, yellow, _, _, _, cigar), Houses),
    %-- hint 7
    member(house(_, _, germany, _, _, waterpipe), Houses),
    %-- hint 8
    member(house(3, _, _, _, milk, _), Houses),
    %-- hint 9 - next_to
    member(house(WATER, _, _, _, water, _), Houses),
    next_to(LIGHT, WATER, N),
    %-- hint 10
    member(house(_, _, _, bird, _, nofilter), Houses),
    %-- hint 11
    member(house(_, _, sweden, dog, _, _), Houses),
    %-- hint 12 - next_to
    member(house(NORWAY, _, norway, _, _, _), Houses),
    member(house(BLUE, blue, _, _, _, _), Houses),
    next_to(NORWAY, BLUE, N),
    %-- hint 13 - next_to
    member(house(HORSE, _, _, horse, _, _), Houses),
    next_to(HORSE, GREEN, N),
    %-- hint 14
    member(house(_, _, _, _, beer, menthol), Houses),
    %-- hint 15
    member(house(_, green, _, _, coffee, _), Houses),

    %-- FINAL QUESTION - WHO LET THE FISH OUT?
    member(house(_, _, _, fish, _, _), Houses),
    member(house(_, _, Who, fish, _, _), Houses).

I tried a lot of combination but:

?- fish(Who).
false.

Edit:
Code is working now, what i changed:

1* From:

%-- hint 5 - next_to
member(house(LIGHT, _, _, _, _, light), Houses),
member(house(CAT, _, _, cat, _, light), Houses),

To:

%-- hint 5 - next_to
    member(house(LIGHT, _, _, _, _, light), Houses),
    member(house(CAT, _, _, cat, _, _), Houses),

2* From:

%-- hint 13 - next_to   
member(house(HORSE, _, _, horse, _, _), Houses),
next_to(HORSE, GREEN, N),

To:

%-- hint 13 - next_to
member(house(YELLOW, yellow, _, _, _, _), Houses),
member(house(HORSE, _, _, horse, _, _), Houses),
next_to(HORSE, YELLOW, N),

If you are reading this look at @Enigmativity comment about structures in helper predicates aswell.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You had two errors in your clues - the first you fixed already with the light smoker. The second is that the horse owner lives next to the yellow house, not the green.

Now, my prolog choked on the + operator so I recoded your helper predicates. This is what I did:

first(H,[H|_]).

on_the_left(X,Y,[X,Y|_]).
on_the_left(X,Y,[_|Hs]) :- on_the_left(X,Y,Hs).

next_to(X,Y,[X,Y|_]).
next_to(X,Y,[Y,X|_]).
next_to(X,Y,[_|Hs]) :- next_to(X,Y,Hs).

middle(X,[_,_,X,_,_]).

Now the puzzle worked nicely with these clues:

fish(Who) :-
    Houses = [
        house(_Color1, _From1, _Animal1, _Drink1, _Smoke1),
        house(_Color2, _From2, _Animal2, _Drink2, _Smoke2),
        house(_Color3, _From3, _Animal3, _Drink3, _Smoke3),
        house(_Color4, _From4, _Animal4, _Drink4, _Smoke4),
        house(_Color5, _From5, _Animal5, _Drink5, _Smoke5) ],
    first(house(_, norway, _, _, _), Houses), %-- hint 1
    member(house(red, england, _, _, _), Houses), %-- hint 2
    on_the_left(house(green, _, _, _, _), house(white, _, _, _, _), Houses), %-- hint 3 - on_the_left
    member(house(_, denmark, _, tea, _), Houses), %-- hint 4
    next_to(house(_, _, _, _, light), house( _, _, cat, _, _), Houses), %-- hint 5 - next_to
    member(house(yellow, _, _, _, cigar), Houses), %-- hint 6
    member(house(_, germany, _, _, waterpipe), Houses), %-- hint 7
    middle(house(_, _, _, milk, _), Houses), %-- hint 8
    next_to(house(_, _, _, _, light), house(_, _, _, water, _), Houses), %-- hint 9 - next_to
    member(house(_, _, bird, _, nofilter), Houses), %-- hint 10
    member(house(_, sweden, dog, _, _), Houses), %-- hint 11
    next_to(house(_, norway, _, _, _), house(blue, _, _, _, _), Houses), %-- hint 12 - next_to
    next_to(house(_, _, horse, _, _), house(yellow, _, _, _, _), Houses), %-- hint 13 - next_to
    member(house(_, _, _, beer, menthol), Houses), %-- hint 14
    member(house(green, _, _, coffee, _), Houses), %-- hint 15
    member(house(_, Who, fish, _, _), Houses),
    write(Houses), nl.

I got:

[house(yellow, norway, cat, water, cigar), house(blue, denmark, horse, tea, light), house(red, england, bird, milk, nofilter), house(green, germany, fish, coffee, waterpipe), house(white, sweden, dog, beer, menthol)]
germany

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...