Search code examples
prologlogiczebra-puzzle

Prolog: Solving a Puzzle


I am new to prolog and I am trying to solve the following question. I am having difficulty trying to understand the logic to solve the problem. I know its similar to zebra problem but, I am unsure how to approach. Any help would be greatly appreciated.

The answers submitted by five students to a T/F quiz are as follows.

Teresa: T T F T F
Tim:    F T T T F
Tania:  T F T T F
Tom:    F T T F T
Tony:   T F T F T
  1. Tania got more answers right than Teresa did.
  2. Tom got more right than Tim.
  3. Tony did not get all the answers right, nor did he get them all wrong.

Write a Prolog program quiz(Answer) that asserts Answer is the list of t and f constants that is the correct answer to the quiz..


Solution

  • If you use SWI-Prolog, you can use library clpfd to solve the puzzle :, I get only one solution (f,f,t,f,t).

    You have a solution [A,B,C,D,E]. You initialize the possibles solutions with

    [A,B,C,D,E] ins 0..1,
    

    You reify all the answers for teresa for example

    teresea([1,1,0,1,0]).
    A #= 1 #<==> TA
    B #= 1 #<==> TB
    .....
    

    you compute the sum of Tis

    sum([TA, TB, ...], #= , Steresa),
    

    and later you will have for Tania got more answers right than Teresa did.

    Stania #> Steresa
    

    You get the solution with

    label([A,B,C,D,E]).
    

    Hope this helps