/* Wijuko grid puzzle in Picat. Place 1-9 in the grid, obeying the sums between pairs of squares. Here's the template, where X is a square and n is a number between 1 and 9. X n X n X n n n X n X n X n n n X n X n X See Martin J. Chlond: "Puzzle - IP in the I" http://pubsonline.informs.org/doi/pdf/10.1287/ited.2015.0142 This Picat model 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 => problem(1,D), wujiko(D, X), foreach(Row in X) println(Row.to_list()) end, nl. go2 => foreach(P in 1..2) println(problem=P), problem(P,D), wujiko(D, X), foreach(Row in X) println(Row.to_list()) end, nl end, nl. wujiko(D, X) => N = D[1].length, % decision variables X = new_array(N,N), X :: 1..N*N, all_different(X.vars()), % row constraints foreach(I in 1..N, J in 1..2) X[I,J] + X[I,J+1] #= D[(I-1)*2+1,J] end, % column constraints foreach(I in 1..2,J in 1..N) X[I,J] + X[I+1,J] #= D[(I-1)*2+2,J] end, solve(X.vars()). % % Problem instance from Chlond's article: % % X _ X 7 X % % 12 8 13 % % X _ X 14 X % % 7 _ 15 % % X _ X _ X % % problem(1,D) => D = [[ _, 7, 0], % row [ 12, 8,13], % column [ _, 14, 0], % row [ 7, _, 15], % column [ _, _, 0]]. % row % another example: % http://www.pressreader.com/search?query=wijuko % Solution: % % 4 9 1 % 8 2 3 % 5 7 6 % problem(2,D) => D = [[ 13, _, 0], % row [ 12,11, _], % column [ _, _, 0], % row [ _, _, 9], % column [ 12,13, 0]]. % row