to be subset sum34

MiniZinc model

int: n;
array[1..n] of string: s;
array[1..n] of int: sl;
array[1..n] of var 0..100: x;
var 0..100: total;

predicate subset_sum(array[int] of int: values,
                     array[int] of var int: x,
                     var int: tot) =
    sum(i in 1..n) (values[i]*x[i]) = tot /\ 
    forall(i in 1..n) (x[i] >= 0) 
;

solve satisfy;

constraint
    subset_sum(sl, x, total) /\
    total = 34               /\ 
    forall(i in 1..n) ( x[i] >= 1 )
;

n = 9;
s  = ["to", "be", "or", "not", "to", "be", "that's", "the", "question"];
sl = [2,    2,    2,    3,     2,    2,    6,        3,      8]; % length

output
[
  if i = 1 /\ j = 1 then "<li>" else "" endif ++
  show(s[i]) ++ " "
  | i in 1..n, j in 1..fix(x[i])
]
++
[ "(" ++ show(x) ++ ")\n"];

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