Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving a logic puzzle using Prolog

Tags:

puzzle

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.

like image 892
user198729 Avatar asked Dec 21 '09 09:12

user198729


2 Answers

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.
like image 95
Volodymyr Gubarkov Avatar answered Oct 12 '22 19:10

Volodymyr Gubarkov


A similar problem and corresponding solution can also be found here:

https://github.com/LogtalkDotOrg/logtalk3/blob/master/examples/puzzles/jam_thief.lgt

Like the solution posted by Kaarel, is possible to request a justification/explanation for the solution found.

like image 39
Paulo Moura Avatar answered Oct 12 '22 19:10

Paulo Moura