Search code examples
prologzebra-puzzle

Numerical comparision in Einstein Riddle


My version is interested in the name, age, origin and subject of students.

solve :-
    length(X, 6),                  % there are six students
    member([manuel, 19, _, _], X), % Manuel is 19 years old
    member([_, 20, _, win], X),    % 20 years old student studies win
    ...

But there are rules I don't know how to implement in Prolog. For example:

Oliver is two years older than the math student but two years younger than the student from Washington.

How do I create a rule where I can compare the age like that?


Solution

  • member([oliver,OLIVER_AGE,_,_],X) ,
    member([_,MATH_AGE,_,math],X) ,
    member([_,WASHINGTON_AGE,washington,_],X) ,
    OLIVER_AGE is MATH_AGE + 2 ,
    OLIVER_AGE is WASHINGTON_AGE - 2