/* Sumbrero grid puzzle in Picat. http://www.omegajunior.net/code/sumbrero/ This model was inspired by the discussion in comp.lang.forth: https://groups.google.com/forum/#!msg/comp.lang.forth/ZaqK8QgroMI/ZhJyVWCz_XYJ Solutions: problem = 1 [1,3,7,2,6,7,9] problem = 2 [3,4,5,1,2,6,7] 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 => member(P,1..2), println(problem=P), data(P,A,Sums,UseAllDiff), sumbrero(A,Sums,UseAllDiff, X), println(X), nl, fail, nl. sumbrero(A,Sums,UseAllDiff, X) => Rows = A.len, Cols = A[1].len, X = new_list(Cols), X :: 1..9, foreach(I in 1..Rows) sum([X[J]*A[I,J] : J in 1..Cols]) #= Sums[I] end, if UseAllDiff then all_different(X) end, solve(X). % % data % % % Sumbrero 7 % http://www.omegajunior.net/code/sumbrero/ % % Problem instance from % https://groups.google.com/forum/#!msg/comp.lang.forth/ZaqK8QgroMI/ZhJyVWCz_XYJ % data(1,A,Sums,UseAllDiff) => A = [ % 1 2 3 4 5 6 7 % A B C D E F G [1,0,0,0,0,1,0], % 1 [0,1,0,1,0,0,1], % 2 [0,0,1,0,1,0,0], % 3 [1,0,0,1,0,0,0], % 4 [0,1,0,0,1,1,0], % 5 [0,0,1,0,0,0,1], % 6 [0,0,0,1,1,0,0], % 7 [1,1,1,0,0,0,0], % 8 [0,0,0,0,0,1,1] % 9 ], Sums = [8,14,13,3,16,16,8,11,16], UseAllDiff = false. % % Problem instance from % https://groups.google.com/forum/#!msg/comp.lang.forth/ZaqK8QgroMI/ZhJyVWCz_XYJ % http://www.omegajunior.net/code/sumbrero/examples.html, Example 1 % "Example 1 'Different numbers from 1 to 7'" % data(2,A,Sums,UseAllDiff) => A = [ % 1 2 3 4 5 6 7 % A B C D E F G [1,0,0,0,0,1,0], % 1 [0,1,0,1,0,0,1], % 2 [0,0,1,0,1,0,0], % 3 [1,0,0,1,0,0,0], % 4 [0,1,0,0,1,1,0], % 5 [0,0,1,0,0,0,1], % 6 [0,0,0,1,1,0,0], % 7 [1,1,1,0,0,0,0], % 8 [0,0,0,0,0,1,1] % 9 ], Sums = [9,12,7,4,12,12,3,12,13], UseAllDiff = true.