Einstein Riddle with List of terms

前端 未结 2 1711
栀梦
栀梦 2020-12-12 02:35

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 pr

2条回答
  •  感动是毒
    2020-12-12 03:25

    just to show an alternative encoding scheme:

    solve :- solve(Sol, From), writeln(From), maplist(writeln, Sol).
    
    solve(Sol, From) :-
        phrase( (from(1, norway)
            ,color(red) = from(england)
            ,color(GREEN, green), color(WHITE, white), {GREEN is WHITE-1}
            ,from(denmark) = drink(tea)
            ,smoke(Light, light), animal(Cat, cat), next_to(Light, Cat)
            ,color(Yellow, yellow), smoke(Yellow, cigar)
            ,from(germany) = smoke(waterpipe)
            ,drink(3, milk)
            ,drink(Water, water), next_to(Light, Water)
            ,animal(bird) = smoke(nofilter)
            ,from(sweden) = animal(dog)
            ,from(NORWAY, norway), color(BLUE, blue), next_to(NORWAY, BLUE)
            ,animal(HORSE, horse), next_to(HORSE, GREEN) % next_to(HORSE, Yellow)
            ,drink(beer) = smoke(menthol)
            ,color(green) = drink(coffee)
            ,animal(Fish, fish), from(Fish, From)
        ), [[1,_,_,_,_,_],
            [2,_,_,_,_,_],
            [3,_,_,_,_,_],
            [4,_,_,_,_,_],
            [5,_,_,_,_,_]
        ], Sol).
    
    state(S), [A,B,C,D,E] --> [A,B,C,D,E], {member(S, [A,B,C,D,E])}.
    
    color(A, B)  --> state([A,B,_,_,_,_]).
    from(A, B)   --> state([A,_,B,_,_,_]).
    animal(A, B) --> state([A,_,_,B,_,_]).
    drink(A, B)  --> state([A,_,_,_,B,_]).
    smoke(A, B)  --> state([A,_,_,_,_,B]).
    
    X = Y --> {
        X=..[Fx|Ax], Y=..[Fy|Ay],
        Xs=..[Fx,S|Ax], Ys=..[Fy,S|Ay]
    }, call(Xs), call(Ys).
    
    next_to(X, Y) --> {1 is abs(X-Y)}.
    

提交回复
热议问题