Prolog Logic/Einstein Puzzle

后端 未结 5 995
我寻月下人不归
我寻月下人不归 2021-01-29 01:26

The problem is

Brown, Clark, Jones and Smith are four substantial citizens who serve the community as architect, banker, doctor and lawyer, though not necessarily respe

5条回答
  •  闹比i
    闹比i (楼主)
    2021-01-29 01:42

    My solution based off what CapelliC said

    % [name,job,conservative,golf,income,age]
    % conserative: 1 = least conservative, 4 = most conservative
    % golf: 1 = worst golfer, 4 = best golfer
    % income: 1 = lowest income, 4 = highest income
    % age: 1 = youngest, 4 = oldest
    
    jobs(L) :- L = 
           [[brown,J1,C1,G1,I1,A1],
            [clark,J2,C2,G2,I2,A2],
            [jones,J3,C3,G3,I3,A3],
            [smith,J4,C4,G4,I4,A4]],
    
            permutation([1,2,3,4], [C1,C2,C3,C4]),
            permutation([1,2,3,4], [I1,I2,I3,I4]),
            permutation([1,2,3,4], [A1,A2,A3,A4]),
            permutation([1,2,3,4], [G1,G2,G3,G4]),
            permutation([banker,architect,doctor,lawyer], [J1,J2,J3,J4]),
    
            % Brown is more conservative than Jones. Brown is less conservative than Smith.
            member([brown,_,CB,GB,IB,AB],L),
            member([jones,_,CJ,_,_,_],L),
            member([smith,_,CS,_,_,_],L),
            CB > CJ,
            CB < CS,
    
            % Brown is a better golfer than those older than him.
            member([_,_,_,G01,_,A01],L),
            (A01 > AB, GB > G01 ; A01 < AB),
            member([_,_,_,G02,_,A02],L),
            (A02 > AB, GB > G02 ; A02 < AB),
            member([_,_,_,G03,_,A03],L),
            (A03 > AB, GB > G03 ; A03 < AB),
    
            vardiff(G01,G02,G03),
            vardiff(G01,A02,A03),
    
            % Brown has a higher income than those younger than Clark.
            member([clark,_,_,_,_,AC],L),
            member([_,_,_,_,I01,A04],L),
            (A04 < AC, IB > I01; AC < A04),
    
            % Banker has a higher income than architect. Banker is neither youngest nor oldest.
            member([_,banker,_,_,IBa,ABa],L),
            member([_,architect,CAr,_,IAr,_],L),
            IBa > IAr,
            (ABa \= 1, ABa \= 4),
    
            % Doctor is a worse golfer than lawyer. Doctor is less conservative than architect.
            member([_,doctor,CDo,GDo,_,_],L),
            member([_,lawyer,_,GLa,_,_],L),
            GDo < GLa,
            CDo < CAr,
    
            % Oldest is most conservative and has highest income.
            member([_,_,4,_,4,4],L),
    
            % Youngest is the best golfer.
            member([_,_,_,4,_,1],L).
    
    vardiff(A,B,C) :- A\=B, A\=C, B\=C.
    

    I get

    3 ?- jobs(L).
    L = [[brown,architect,2,4,1,1],[clark,banker,3,1,2,2],[jones,doctor,1,2,3,3],[smith,lawyer,4,3,4,4]] ;
    L = [[brown,architect,2,4,1,1],[clark,banker,3,2,2,2],[jones,doctor,1,1,3,3],[smith,lawyer,4,3,4,4]] ;
    L = [[brown,architect,2,4,1,1],[clark,banker,3,3,2,2],[jones,doctor,1,1,3,3],[smith,lawyer,4,2,4,4]] ;
    L = [[brown,architect,2,4,1,1],[clark,banker,3,1,2,3],[jones,doctor,1,2,3,2],[smith,lawyer,4,3,4,4]] ;
    L = [[brown,architect,2,4,1,1],[clark,banker,3,2,2,3],[jones,doctor,1,1,3,2],[smith,lawyer,4,3,4,4]] ;
    L = [[brown,architect,2,4,1,1],[clark,banker,3,3,2,3],[jones,doctor,1,1,3,2],[smith,lawyer,4,2,4,4]] ;
    L = [[brown,architect,2,4,1,1],[clark,banker,3,1,3,2],[jones,doctor,1,2,2,3],[smith,lawyer,4,3,4,4]] ;
    L = [[brown,architect,2,4,1,1],[clark,banker,3,2,3,2],[jones,doctor,1,1,2,3],[smith,lawyer,4,3,4,4]] ;
    L = [[brown,architect,2,4,1,1],[clark,banker,3,3,3,2],[jones,doctor,1,1,2,3],[smith,lawyer,4,2,4,4]] ;
    L = [[brown,architect,2,4,1,1],[clark,banker,3,1,3,3],[jones,doctor,1,2,2,2],[smith,lawyer,4,3,4,4]] ;
    L = [[brown,architect,2,4,1,1],[clark,banker,3,2,3,3],[jones,doctor,1,1,2,2],[smith,lawyer,4,3,4,4]] ;
    L = [[brown,architect,2,4,1,1],[clark,banker,3,3,3,3],[jones,doctor,1,1,2,2],[smith,lawyer,4,2,4,4]] ;
    

    Multiple answers that repeat so I deleted some of them.

    All the clues are satisfied except when Clark is the second oldest for example

    L = [[brown,architect,2,4,1,1],[clark,banker,3,1,2,3],[jones,doctor,1,2,3,2],[smith,lawyer,4,3,4,4]] ;
    

    This clue is violated

    % Brown has a higher income than those younger than Clark.
    

    And for all of my answers Brown is the youngest so clues like

    % Brown is a better golfer than those older than him.
    % Brown has a higher income than those younger than Clark.
    % Youngest is the best golfer.
    

    seem a bit pointless...

提交回复
热议问题