1
0
This repository has been archived on 2025-03-06. You can view files and clone it, but cannot push or open issues or pull requests.
Jip J. Dekker f2a1c4e389 Squashed 'software/mza/' content from commit f970a59b17
git-subtree-dir: software/mza
git-subtree-split: f970a59b177c13ca3dd8aaef8cc6681d83b7e813
2021-07-11 16:34:30 +10:00

128 lines
5.0 KiB
MiniZinc

% RUNS ON mzn_mer_fd
% RUNS ON mzn_mer_lazyfd
% RUNS ON mzn_mer_lp
% RUNS ON zinc_fdic_mznlib
% RUNS ON zinc_mip_hl
% RUNS ON minizinc_cpx
% RUNS ON minizinc_fd
%
%-----------------------------------------------------------------------------%
% Sudoku for squares of arbitrary size N = (S x S)
%-----------------------------------------------------------------------------%
%int: S=3;
%int: N = S * S;
%array[1..N,1..N] of int: puzzle_input=[|
%0, 0, 0, 0, 0, 0, 0, 0, 0|
%0, 6, 8, 4, 0, 1, 0, 7, 0|
%0, 0, 0, 0, 8, 5, 0, 3, 0|
%0, 2, 6, 8, 0, 9, 0, 4, 0|
%0, 0, 7, 0, 0, 0, 9, 0, 0|
%0, 5, 0, 1, 0, 6, 3, 2, 0|
%0, 4, 0, 6, 1, 0, 0, 0, 0|
%0, 3, 0, 2, 0, 7, 6, 9, 0|
%0, 0, 0, 0, 0, 0, 0, 0, 0|
%|];
int: S;
int: N=S*S;
array[1..N,1..N] of int: puzzle_input;
%int: S=4;
%int: N=S*S;
%array[1..N,1..N] of int: puzzle_input=[|
%13, 4, 0, 16, 0, 2, 0, 0, 0, 0, 0, 0, 11, 0, 14, 10|
%9, 12, 14, 5, 7, 0, 0, 15, 0, 0, 0, 0, 0, 2, 1, 0|
%2, 15, 0, 6, 16, 0, 10, 9, 0, 0, 0, 0, 0, 5, 3, 0|
%0, 0, 1, 0, 5, 0, 14, 0, 0, 0, 0, 0, 9, 6, 0, 0|
%0, 0, 0, 0, 10, 12, 0, 5, 0, 0, 13, 0, 0, 0, 6, 8|
%0, 0, 0, 0, 8, 0, 9, 0, 2, 0, 0, 14, 16, 0, 0, 11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 25, 18|
%21, 25, 1, 5, 0, 0, 0, 0, 0, 2, 0, 17, 0, 14, 18, 0, 4, 0, 7, 0, 0, 0, 0, 0, 16|
%8, 19, 0, 15, 0, 0, 9, 0, 20, 21, 25, 0, 3, 0, 11, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0|
%0, 0, 0, 0, 2, 0, 0, 7, 0, 0, 0, 22, 0, 0, 9, 17, 14, 0, 23, 0, 8, 4, 0, 0, 6|
%0, 4, 0, 0, 6, 18, 0, 3, 10, 17, 8, 0, 0, 0, 0, 0, 0, 0, 0, 12, 21, 22, 0, 0, 7|
%0, 0, 17, 0, 0, 5, 0, 4, 25, 0, 12, 19, 20, 13, 0, 0, 0, 0, 0, 1, 0, 14, 9, 0, 11|];
array[1..N*N] of var 1..N: puzzle;
constraint forall (i in 1..N, j in 1..N) (
if (puzzle_input[i,j]==0) then true
else puzzle[(i-1)*N+j]==puzzle_input[i,j]
endif
);
ann: total;
function var int: eq_const(var int: x, int: d) ::total =
if d in dom(x) then let {
array[int] of var int: i2ax = int2a(x)
} in i2ax[d] else 0 endif;
function array[int] of var int: int2a(var int: x) ::total =
let {
array[dom(x)] of var 0..1: a;
constraint int_lin_eq([1|i in index_set(a)],a,1);
constraint int_lin_eq([i|i in index_set(a)]++[-1],a++[x],0);
} in a;
predicate alldiff(array[int] of var int: x) =
forall (d in dom_array(x)) (
int_lin_le([1|i in index_set(x)],[eq_const(x[i],d) | i in index_set(x)],1)
);
predicate alldiff_builtin(array[int] of var int: x);
% All cells in a row, in a column, and in a subsquare are different.
constraint
forall(i in 1..N)( alldiff(j in 1..N)( puzzle[(i-1)*N+j] ))
/\
forall(j in 1..N)( alldiff(i in 1..N)( puzzle[(i-1)*N+j] ))
/\
forall(i,j in 1..S)
( alldiff(p,q in 1..S)( puzzle[(S*(i-1)+p-1)*N+(S*(j-1)+q)] ))
;
solve satisfy;
%-----------------------------------------------------------------------------%
%
% The data for the puzzle that causes satz to make 1 backtrack (normally none
% are made).
%