0

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

jahabase
  • 3
  • 2

2 Answers2

1

Let's assume we have a list of persons Persons and a list of liars Liars. You can decompose the problem into two requirements:

  1. Liars is a "sublist" of Persons: We need an additional predicate, e.g. sublist(Persons,Liars):

    sublist([],[]).                               % base case: empty list
    sublist([H|T],[H|Rest]) :- sublist(T,Rest).   % the sublist may contain H ...
    sublist([_|T],Rest)     :- sublist(T,Rest).   % ... or not
    
  2. There are exactly four liars:

    length(Liars,4)
    

Now you can just put it together with a conjunction:

length(Liars,4),sublist(Persons,Liars)

I put the length(Liars,4) in front because it's deterministic, whereas sublist/2 creates choice-points.

danielp
  • 1,179
  • 11
  • 26
  • Oh wow, this makes alot of sense. Thanks a bunch really, it seems sooo simple yet logical programming to me is very alien-like and hard to get my head around. Seriously thanks for the help! – jahabase Aug 15 '14 at 00:22
0

this code uses SWI-Prolog' library(aggregate) for easy counting...

s(a, b, d, e).
s(b, a, c, e).
s(c, b, f, e).
s(d, a, f, c).
s(e, c, d, f).
s(f, c, d, a).

count_lies(T, N) :-
    aggregate_all(count, (s(_, X,Y,Z), (T==X;T==Y;T==Z)), N).

solve(P) :-
    member(P, [a,b,c,d,e,f]), count_lies(P, 4).

test:

?- solve(P).
P = c ;
false.
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • Sorry to be a nuisance, but can someone explain the "aggregate_all" method? I've looked online but i can't really make sense of it, I do see that this is where x,y,z need to be the same person (and seeing as X,Y,Z are at the last 3 parts in the list, this is the statement of the first person of the list), but what does "count" and "N" do in this function? – jahabase Aug 15 '14 at 00:40