skip to Main Content

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


  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.

    Login or Signup to reply.
  2. 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.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search