to be magic square

MiniZinc model

include "globals.mzn";
int: n = 3;
int: total = ( n * (n*n + 1)) div 2;
array[1..n*n] of string: s =
    ["to", "be", "or", "not", "to", "be", "that's", "the", "question"];
array[1..n,1..n] of var 1..n*n: magic;
solve satisfy;
constraint
  all_different([magic[i,j] | i in 1..n, j in 1..n]) /\
  forall(k in 1..n) (
     sum(i in 1..n) (magic[k,i]) = total /\
     sum(i in 1..n) (magic[i,k]) = total
  ) 
  /\
  sum(i in 1..n) (magic[i,i]) = total 
  /\
  sum(i in 1..n) (magic[i,n-i+1]) = total
;
output 
[
  if i = 1 /\ j = 1 then "<li>" else "" endif ++
    show(s[fix(magic[i,j])]) ++ " "
  | i,j in 1..n
];

Back to Constrained-based constraint poetry
Created by Hakan Kjellerstrand hakank@gmail.com