to be nonogram

tototobetobetotototototototobebebebetoto
totototobetotototototototobebebebebebeto
tototobetobetobetobetotobebetotobebebeto
tototototototobetobetobebetotototobebebe
tototototototobebebetobetotobebetobebebe
tobebebebetotobebebebebetotobebetotobebe
bebebebebebebetobebebetototototototobebe
bebebetototobebebebebetotototobetobebeto
bebetobebetototobebebebetototototobetoto
bebetobebetotototobebebetotobebebebetoto
bebetotototototototobebebebebetotobebeto
bebetotobetototobebebebebetototototobeto
tobebetotototobebetotobebebetotototobeto
totobebebebebebetotototobebetototobebeto
totototototobetotototobebebebebebebetoto
totototototobebetotobebetobebetototototo
tobetototototobebebebetototototototototo
bebebetototototototototototototobetobeto
tobetototototototototototototototobetoto
totototototototototototototototobetobeto

MiniZinc model

include "globals.mzn"; 
int: rows;
int: row_rule_len;
array[1..rows, 1..row_rule_len] of int: row_rules;
int: cols;
int: col_rule_len;
array[1..cols, 1..col_rule_len] of int: col_rules;
array[1..rows, 1..cols] of var 1..2: x;
array[1..2] of string: s = ["to", "be"];
solve satisfy;
predicate make_automaton(array[int] of var int: x, array[int] of int: p) =
  let {
      int: n = length(p),
      int: len = max(length([p[i] | i in 1..n where p[i] > 0]) + sum(p),1),
      int: leading_zeros = sum(i in 1..n) (bool2int(p[i] = 0)),
      set of int: zero_positions = {sum(j in 1..i) (p[j]+1) -leading_zeros | i in 1..n where p[i] > 0},
     array[1..2*len] of 0..len*2: states = 
   if (length([p[i] | i in 1..n where p[i] > 0]) + sum(p)) = 0 then [1,1] else [1, 2] ++
   [
     if i div 2 in zero_positions then
         if i mod 2 = 0 then 0 else (i div 2) + 1
         endif
     elseif (i-1) div 2 in zero_positions then
         if i mod 2 = 0 then (i div 2)+1 else (i div 2)+2
         endif
     else
       if not( (((i-1) div 2) - 1) in zero_positions) then
          if i mod 2 = 0 then (i div 2) + 1
          else 
            if (i div 2) + 1 in zero_positions then (i div 2) + 2  else 0 endif
          endif
        else
           if i mod 2 = 0 then (i div 2) + 1
           else 
              if not((i div 2) + 1 in zero_positions) then 0 else (i div 2) + 2 endif
           endif
        endif
     endif
  | i in 3..2*(len-1)]
  ++
  [len, 0]
  endif } 
  in regular(x,len, 2, array2d(1..len, 1..2, states), 1, {len})
;

constraint
  forall(j in 1..cols) (
    make_automaton([x[i,j] | i in 1..rows], [col_rules[j,k] | k in 1..col_rule_len])
  ) /\
  forall(i in 1..rows) (
    make_automaton([x[i,j] | j in 1..cols], [row_rules[i,k] | k in 1..row_rule_len])
  );

output 
[
  if j = 1 then "\n" else "" endif ++
     if fix(x[i,j]) = 1 then s[1] else s[2] endif
  | i in 1..rows, j in 1..cols
];

% Nonogram problem: P199
% From http://87.230.22.228/examples/nono_regular.ecl.txt
rows = 20;
row_rule_len = 6;
row_rules = array2d(1..rows, 1..row_rule_len, 
 [
  0,0,0,1,1,4,
  0,0,0,0,1,6,
  1,1,1,1,2,3,
  0,0,1,1,2,3,
  0,0,3,1,2,3,
  0,0,4,5,2,2,
  0,0,0,7,3,2,
  0,0,3,5,1,2,
  0,0,2,2,4,1,
  0,0,2,2,3,4,
  0,0,0,2,5,2,
  0,0,2,1,5,1,
  0,0,2,2,3,1,
  0,0,0,6,2,2,
  0,0,0,0,1,7,
  0,0,0,2,2,2,
  0,0,0,0,1,4,
  0,0,0,3,1,1,
  0,0,0,0,1,1,
  0,0,0,0,1,1
  ]);
cols = 20;
col_rule_len = 5;
col_rules = array2d(1..cols, 1..col_rule_len,
 [
  0,0,0,6,1,
  0,0,0,8,3,
  0,0,3,2,1,
  1,1,2,2,1,
  1,2,2,1,1,
  0,1,1,1,1,
  0,0,0,2,3,
  0,4,1,2,2,
  0,0,5,2,1,
  0,0,8,1,1,
  0,0,0,7,2,
  0,0,3,5,2,
  0,0,0,2,5,
  0,0,2,1,4,
  0,2,2,2,2,
  2,2,1,1,1,
  3,1,1,1,1,
  0,5,4,2,1,
  0,7,4,1,1,
  0,0,0,0,4
  ]);

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