/* Keep it even in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 110. Keep it even Take 16 objects (pieces of paper, coins, plums, checkers) and put them in 4 rows of 4 each. Remove 6, leaving an even number of objects in each row and each column. How many solutions are there? (puzzle 21 from Kordemsky (1992)) (Fig. 11.2) """ There are 96 solutions, for example: [0,0,1,1] [0,1,0,1] [1,0,0,1] [1,1,1,1] [0,0,1,1] [0,1,0,1] [1,1,1,1] [1,0,0,1] [0,0,1,1] [0,1,1,0] [1,0,1,0] [1,1,1,1] [0,0,1,1] [0,1,1,0] [1,1,1,1] [1,0,1,0] This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => keep_it_even(X), foreach(Row in X) println(Row.to_list) end, nl, fail, nl. % The the number of solutions. go2 => println(count_all(keep_it_even(_X))). keep_it_even(X) => N = 4, X = new_array(N,N), X :: 0..1, Vars = X.vars, sum(Vars) #= N*N - 6, foreach(I in 1..N) sum([X[I,J] : J in 1..N]) mod 2 #= 0, sum([X[J,I] : J in 1..N]) mod 2 #= 0 end, solve(Vars).