/* True/False puzzle in Picat. From http://stackoverflow.com/questions/28224626/prolog-solving-a-puzzle """ The answers submitted by five students to a T/F quiz are as follows. Teresa: T T F T F Tim: F T T T F Tania: T F T T F Tom: F T T F T Tony: T F T F T 1. Tania got more answers right than Teresa did. 2. Tom got more right than Tim. 3. Tony did not get all the answers right, nor did he get them all wrong. Write a Prolog program quiz(Answer) that asserts Answer is the list of t and f constants that is the correct answer to the quiz.. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => All = findall(X, tf_puzzle_cp(X)), println(All), nl. go2 => All = findall(X, tf_puzzle(X)), println(All), nl. tf_puzzle(X) => N = 5, X = new_list(N), foreach(I in 1..N) member(X[I], [t,f]) end, % 1. Tania got more answers right than Teresa did. tania(Tania), teresa(Teresa), match(X,Tania) > match(X,Teresa), % 2. Tom got more right than Tim. tom(Tom), tim(Tim), match(X,Tom) > match(X,Tim), % 3. Tony did not get all the answers right, nor did he get them all wrong. tony(Tony), TonySum = match(X,Tony), TonySum > 1, TonySum < N. match(Xs, Person) = sum([1: ({X,P} in zip(Xs,Person)), X == P]). teresa(L) => L = [t,t,f,t,f]. tim(L) => L = [f,t,t,t,f]. tania(L) => L = [t,f,t,t,f]. tom(L) => L = [f,t,t,f,t]. tony(L) => L = [t,f,t,f,t]. % CP approach tf_puzzle_cp(X) => T = 1, F = 0, Teresa = [T,T,F,T,F], Tim = [F,T,T,T,F], Tania = [T,F,T,T,F], Tom = [F,T,T,F,T], Tony = [T,F,T,F,T], N = Teresa.length, X = new_list(N), X :: 0..1, % 1. Tania got more answers right than Teresa did. sum([Tania[I] #= X[I] : I in 1..N]) #> sum([Teresa[I] #= X[I] : I in 1..N]), % 2. Tom got more right than Tim. sum([Tom[I] #= X[I] : I in 1..N]) #> sum([Tim[I] #= X[I] : I in 1..N]), % 3. Tony did not get all the answers right, nor did he get them all wrong. TonySum #= sum([Tony[I] #= X[I] : I in 1..N]), TonySum #> 1, TonySum #< N, solve(X).