/* Tricky Multiple Choice Question in Picat. % http://math.stackexchange.com/questions/2217248/which-answer-in-this-list-is-the-correct-answer-to-this-question % """ % Which answer in this list is the correct answer to this question? % % 1. All of the below. % 2. None of the below. % 3. All of the above. % 4. One of the above. % 5. None of the above. % 6. None of the above. % """ Below is some different approaches using the CP module. 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. % % Using just boolean operators #/\ (and) and #\/ (or) % go ?=> N = 6, X = new_list(N), X :: 0..1, % 1. All of the below. (X[1] #<=> X[2] #/\ X[3] #/\ X[4] #/\ X[5] #/\ X[6]), % 2. None of the below. (X[2] #<=> #~(X[3] #\/ X[4] #\/ X[5] #\/ X[6])), % 3. All of the above. (X[3] #<=> X[1] #/\ X[2]), % 4. One of the above. (X[4] #<=> X[1] #\/ X[2] #\/ X[3]), % 5. None of the above. (X[5] #<=> #~(X[1] #\/ X[2] #\/ X[3] #\/ X[4])), % 6. None of the above. (X[6] #<=> #~(X[1] #\/ X[2] #\/ X[3] #\/ X[4] #\/ X[5])), solve(X), println(X), foreach(I in 1..N) if X[I] == 1 then printf("Answer %d is correct\n",I) end end, fail, nl. go => true. % % Alternative approach, using "plain" sum. % go2 ?=> N = 6, X = new_list(N), X :: 0..1, % 1. All of the below. X[1] #<=> sum([X[I] : I in 2..6]) #= 5, % 2. None of the below. X[2] #<=> sum([X[I] : I in 3..6]) #= 0, % 3. All of the above. X[3] #<=> sum([X[I] : I in 1..2]) #= 2, % 4. One of the above. X[4] #<=> sum([X[I] : I in 1..3]) #= 1, % 5. None of the above. X[5] #<=> sum([X[I] : I in 1..4]) #= 0, % 6. None of the above. X[6] #<=> sum([X[I] : I in 1..5]) #= 0, solve(X), println(X), foreach(I in 1..N) if X[I] == 1 then printf("Answer %d is correct\n",I) end end, fail, nl. go2 => true. % % Variant of go2/0. % go3 ?=> N = 6, X = new_list(N), X :: 0..1, % 1. All of the below. sum_range(1,X,2..6,5), % 2. None of the below. sum_range(2,X,3..6,0), % 3. All of the above. sum_range(3,X,1..2,2), % 4. One of the above. sum_range(4,X,1..3,1), % 5. None of the above. sum_range(5,X,1..4,0), % 6. None of the above. sum_range(6,X,1..5,0), solve(X), println(X), foreach(I in 1..N) if X[I] == 1 then printf("Answer %d is correct\n",I) end end, fail, nl. go3 => true. % % Yet another approach. % go4 ?=> N = 6, X = new_list(N), X :: 0..1, % 1. All of the below. all_below(X,1), % 2. None of the below. all_below(X,2), % 3. All of the above. all_above(X,3), % 4. One of the above. n_above(X,4,1), % 5. None of the above. none_above(X,5), % 6. None of the above. none_above(X,6), solve(X), println(X), foreach(I in 1..N) if X[I] == 1 then printf("Answer %d is correct\n",I) end end, fail, nl. go4 => true. sum_range(I, X,List, Value) => X[I] #<=> sum([X[J] : J in List]) #= Value. n_above(X,I,N) => X[I] #<=> sum([X[J] : J in 1..I-1]) #= N. all_above(X,I) => n_above(X,I,I-1). none_above(X,I) => n_above(X,I,0). n_below(X,I,N) => X[I] #<=> sum([X[J] : J in I+1..X.len]) #= N. all_below(X,I) => n_below(X,I,X.len-I-1). none_below(X,I) => n_below(X,I,0).