/* Roses, shamrocks, and thistles in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 86. Roses, shamrocks, and thistles Place the numbers 1 to 12 (one number in every design) so that they shall add up to the same sum in the following seven different ways: each of the two center columns, each of the two central rows, the four roses together, the four shamrocks together, and the four thistles together. (puzzle 400 from Dudeney 2016) s s t r r t t r r t s s """ Here are three of the 24960 solutions: sum = 26 _ 11 12 _ 9 8 6 3 10 5 7 4 _ 2 1 _ sum = 26 _ 11 1 _ 9 8 6 3 10 5 7 4 _ 2 12 _ sum = 26 _ 2 12 _ 9 8 6 3 10 5 7 4 _ 11 1 _ 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 => A = [[0,s,s,0], [t,r,r,t], [t,r,r,t], [0,s,s,0]], p(A,X,Sum), N = A.len, println(sum=Sum), foreach(I in 1..N) foreach(J in 1..N) if X[I,J] == 0 then print(" _ ") else printf("%2d ",X[I,J]) end end, nl end, nl, fail, nl. /* There are 24960 different solutions. */ go2 => A = [[0,s,s,0], [t,r,r,t], [t,r,r,t], [0,s,s,0]], C = count_all(p(A,_X,_Sum)), println(c=C), nl. p(A,X,Sum) => N = A.len, X = new_array(N,N), X :: 0..12, Sum :: 0..N*N*N, all_different_except_0(X.vars), foreach(I in 1..N, J in 1..N) if A[I,J] == 0 then X[I,J] #= 0 else X[I,J] #> 0 end end, foreach(I in 2..N-1) sum([X[I,J] : J in 1..N]) #= Sum, sum([X[J,I] : J in 1..N]) #= Sum, end, foreach(S in [r,s,t]) sum([X[I,J] : I in 1..N, J in 1..N, A[I,J] == S]) #= Sum end, Vars = X.vars ++ [Sum], solve($[degree,updown],Vars).