/* How many liars? in Picat. From MindYourDecisions """ There are 5 people. Each person is either a truth-teller, who always tells the truth, or a liar, who always lies. Each person knows the type of everyone else. Each person is asked, "How many liars are among you?" The five answers are: "One." "Two." "Three." "Four." "Five." How many liars are there? """ There are 4 liars. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. import cp. main => go. go ?=> N = 5, X = new_list(N), X :: 0..1, foreach(I in 1..N) X[I] #= 1 #<=> sum([X[J] #= 0 : J in 1..N]) #= I end, solve(X), println(X=get_liars(X)), fail, nl. go => true. /* Let's generalize this: There are N people (N > 1), and the N answers are One Two ... N-1 N The general soluton: There are N-1 liars and the N-1'th person tells the truth. 1 = no_solution 2 = [1,0] = 1 3 = [0,1,0] = 2 4 = [0,0,1,0] = 3 5 = [0,0,0,1,0] = 4 6 = [0,0,0,0,1,0] = 5 7 = [0,0,0,0,0,1,0] = 6 8 = [0,0,0,0,0,0,1,0] = 7 9 = [0,0,0,0,0,0,0,1,0] = 8 10 = [0,0,0,0,0,0,0,0,1,0] = 9 */ go2 ?=> member(N,1..10), X = new_list(N), X :: 0..1, foreach(I in 1..N) X[I] #= 1 #<=> sum([X[J] #= 0 : J in 1..N]) #= I end, if solve(X) then println(N=X=get_liars(X)) else println(N=no_solution) end, fail, nl. go2 => true. get_liars(X) = [I : I in 1..X.len, X[I] == 1].first.