to be kakuro

question that's   _        _        the      that's   question
the      question _        the      question to2      that's  
be2      the      to2      question that's   _        _       
_        be2      to1      _        be1      be2      _       
_        _        not      be2      to1      or       be1     
the      question or       to1      _        to1      not     
or       to1      be1      _        _        be1      to1     

MiniZinc model

include "globals.mzn"; 
int: n = 7;
int: B = -1; % Blank in the hint grids
int: num_row_hints = 12;
int: num_col_hints = 12;
array[1..n, 1..n] of int: hints_row = array2d(1..n, 1..n,
[
%  1  2  3  4  5  6  7
   1, 1, B, B, 2, 2, 2, % 1
   3, 3, B, 4, 4, 4, 4, % 2
   5, 5, 5, 5, 5, B, B, % 3
   B, 6, 6, B, 7, 7, B, % 4
   B, B, 8, 8, 8, 8, 8, % 5
   9, 9, 9, 9, B,10,10, % 6
  11,11,11, B, B,12,12, % 7
]);
array[1..n, 1..n] of int: hints_col = array2d(1..n, 1..n,
[
% 1  2  3  4  5  6  7
 13,15, B, B,20,21,23, % 1
 13,15, B,18,20,21,23, % 2
 13,15,17,18,20, B, B, % 3
  B,15,17, B,20,22, B, % 4
  B, B,17,19,20,22,24, % 5
 14,16,17,19, B,22,24, % 6
 14,16,17, B, B,22,24, % 7
]);
array[1..num_row_hints] of int: row_hint_sums = 
[ 16, 24, 17, 29, 35, 7, 8, 16, 21, 5, 6, 3];
array[1..num_col_hints] of int: col_hint_sums =
[ 23, 11, 30, 10, 15, 17, 7, 27, 12, 12, 16, 7];
array[1..n] of string: s = ["to1     ", 
                            "be1     ", 
                            "or      ", 
                            "not     ", 
                            "to2     ", 
                            "be2     ", 
                            "that's  ", 
                            "the     ", 
                            "question"];

array[1..n, 1..n] of var 0..9: x;
solve satisfy;
constraint
  forall(i,j in 1..n) (
        if hints_row[i,j] = B then
           x[i,j] = 0
        else
           x[i,j] > 0
        endif
  ) /\
  forall(p in 1..num_row_hints) (
      row_hint_sums[p] = sum(i,j in 1..n where hints_row[i,j] = p) ( x[i,j])
      /\
      alldifferent([x[i,j] | i,j in 1..n where hints_row[i,j] = p])
  ) /\
  forall(p in 1..num_col_hints) (
      col_hint_sums[p] = sum(i,j in 1..n where hints_col[i,j] = p+num_row_hints) ( x[i,j])
      /\
      alldifferent([x[i,j] | i,j in 1..n where hints_col[i,j] = p+num_row_hints])
  );

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

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