Just starting to get into Prolog as artificial intelligence is quite interesting and different than alot of other programming languages out there, and I’ve stumbled upon a logic puzzle:
http://www.brainbashers.com/showpuzzles.as
and wondering how i could start coding such a puzzle?
The thing that’s troubling me quite abit is the fact that we know about bad information how would you strictly select them of a list of people
2
Answers
Let’s assume we have a list of persons
Persons
and a list of liarsLiars
.You can decompose the problem into two requirements:
Liars
is a “sublist” ofPersons
: We need an additional predicate, e.g.sublist(Persons,Liars)
:There are exactly four liars:
Now you can just put it together with a conjunction:
I put the
length(Liars,4)
in front because it’s deterministic, whereassublist/2
creates choice-points.this code uses SWI-Prolog’ library(aggregate) for easy counting…
test: