Search code examples
prologpuzzle

Solving a logic puzzle using Prolog


The criminal is one of A, B, C and D.

A says: "It's not me"
B says: "It's D"
C says: "It's B"
D says: "It's not me"

And we know that only one of them tells the truth.

Who is the one? I want to solve it by using Prolog.

It's an interview question.


Solution

  • One-liner solution

    ?- member(K,[a,b,c,d]),(K\=a->A=1;A=0),(K=d->B=1;B=0),(K=b->C=1;C=0),(K\=d->D=1;D=0),A+B+C+D=:=1.
    K = a,
    A = 0,
    B = 0,
    C = 0,
    D = 1 ;
    false.