79546672

Date: 2025-03-31 15:54:45
Score: 0.5
Natty:
Report link

Ok, guys. Here is my MiniZinc model, giving the correct result.

% Solution for Puzzle
%
% Conditions:
% row 1: sum>=38            column A: sequence asc. or desc.
% row 2: two 6, no 1                column B: two 3 but no more odds
% row 3: two 3              column C: no 2
% row 4: two 5, no 4                column D: two 8
% row 5: three 7, no 2          column E: sum = 21
% row 6: two 8, no 3            column F: two 9

include "globals.mzn";
include "alldifferent.mzn";

set of int: digits = 1..9;
array[1..6,1..6] of var digits: matrix;

% sum of row 1 >= 38
constraint (matrix[1,1]+matrix[1,2]+matrix[1,3]+matrix[1,4]+matrix[1,5]+matrix[1,6]>=38);
% sum of column E = 21
constraint (matrix[1,5]+matrix[2,5]+matrix[3,5]+matrix[4,5]+matrix[5,5]+matrix[6,5]=21);

% === Setting for Rows ===
% All different in row 1
constraint alldifferent( [ matrix[1,j] | j in 1..6 ]); 
% All different except 2x6 in row 2
constraint all_different_except(matrix[2, 1..6], {6});
constraint count([matrix[2, j] | j in 1..6], 6) = 2;
% No 1 in row 2
constraint forall(j in 1..6) (matrix[2,j] != 1);
% All different except 2x3 in row 3
constraint all_different_except(matrix[3, 1..6], {3});
constraint count([matrix[3, j] | j in 1..6], 3) = 2;
% All different except 2x5 in row 4
constraint all_different_except(matrix[4, 1..6], {5});
constraint count([matrix[4, j] | j in 1..6], 5) = 2;
% No 4 in row 4
constraint forall(j in 1..6) (matrix[4,j] != 4);
% All different except 3x7 in row 5
constraint all_different_except(matrix[5, 1..6], {7});
constraint count([matrix[5, j] | j in 1..6], 7) = 3;
% No 2 in row 5
constraint forall(j in 1..6) (matrix[5,j] != 2);
% All different except 2x8 in row 6
constraint all_different_except(matrix[6, 1..6], {8});
constraint count([matrix[6, j] | j in 1..6], 8) = 2;
% No 3 in row 6
constraint forall(j in 1..6) (matrix[6,j] != 3);
% Neighbors in rows cannot be the same
constraint forall(i in 1..6) (forall(j in 2..6) (matrix[i,j] != matrix[i,j-1]));

% === Setting for Columnns ===
% All different in columns 1, 3, and 5
constraint alldifferent( [ matrix[i,1] | i in 1..6 ]); 
constraint alldifferent( [ matrix[i,3] | i in 1..6 ]); 
constraint alldifferent( [ matrix[i,5] | i in 1..6 ]); 
% All different except 2x3 in col B
constraint all_different_except(matrix[1..6, 2], {3});
constraint count([matrix[i, 2] | i in 1..6], 3) = 2;
% Only even digits in column B except for the 2 Threes
constraint forall(i in 1..6 where matrix[i,2]!=3) (matrix[i,2]=2 \/ matrix[i,2]=4 \/ matrix[i,2]=6 \/ matrix[i,2]=8);
% No 2 in column C
constraint forall(i in 1..6) (matrix[i,3] != 2);
% All different except 2x8 in col D
constraint all_different_except(matrix[1..6, 4], {8});
constraint count([matrix[i, 4] | i in 1..6], 8) = 2;
% All different except 2x9 in col F
constraint all_different_except(matrix[1..6, 6], {9});
constraint count([matrix[i, 6] | i in 1..6], 9) = 2;
% Neighbors in cols cannot be the same
constraint forall(i in 2..6) (forall(j in 1..6) (matrix[i,j] != matrix[i-1,j]));

% Column A: asc. or desc. Sequence
constraint (matrix[1,1]=matrix[2,1]+1/\matrix[2,1]=matrix[3,1]+1/\matrix[3,1]=matrix[4,1]+1/\
            matrix[4,1]=matrix[5,1]+1/\matrix[5,1]=matrix[6,1]+1) \/
           (matrix[1,1]+1=matrix[2,1]/\matrix[2,1]+1=matrix[3,1]/\matrix[3,1]+1=matrix[4,1]/\
            matrix[4,1]+1=matrix[5,1]/\matrix[5,1]+1=matrix[6,1]);

% All Digits must occur 4x in the matrix
constraint count([matrix[i,j] | i,j in 1..6], 1) = 4;
constraint count([matrix[i,j] | i,j in 1..6], 2) = 4;
constraint count([matrix[i,j] | i,j in 1..6], 3) = 4;
constraint count([matrix[i,j] | i,j in 1..6], 4) = 4;
constraint count([matrix[i,j] | i,j in 1..6], 5) = 4;
constraint count([matrix[i,j] | i,j in 1..6], 6) = 4;
constraint count([matrix[i,j] | i,j in 1..6], 7) = 4;
constraint count([matrix[i,j] | i,j in 1..6], 8) = 4;
constraint count([matrix[i,j] | i,j in 1..6], 9) = 4;

solve satisfy;

output ["S O L U T I O N \n",
        "=============== \n",
        "R/C\t A\t B\t C\t D\t E\t F \n",
        "-------------------------------------------------------\n",
        "1\t \(matrix[1,1])\t \(matrix[1,2])\t \(matrix[1,3])\t \(matrix[1,4])\t \(matrix[1,5])\t \(matrix[1,6])  \n",
        "2\t \(matrix[2,1])\t \(matrix[2,2])\t \(matrix[2,3])\t \(matrix[2,4])\t \(matrix[2,5])\t \(matrix[2,6])  \n",
        "3\t \(matrix[3,1])\t \(matrix[3,2])\t \(matrix[3,3])\t \(matrix[3,4])\t \(matrix[3,5])\t \(matrix[3,6])  \n",
        "4\t \(matrix[4,1])\t \(matrix[4,2])\t \(matrix[4,3])\t \(matrix[4,4])\t \(matrix[4,5])\t \(matrix[4,6])  \n",
        "5\t \(matrix[5,1])\t \(matrix[5,2])\t \(matrix[5,3])\t \(matrix[5,4])\t \(matrix[5,5])\t \(matrix[5,6])  \n",
        "6\t \(matrix[6,1])\t \(matrix[6,2])\t \(matrix[6,3])\t \(matrix[6,4])\t \(matrix[6,5])\t \(matrix[6,6])  \n" ];

I am sure that several code parts could be coded more elegant. But I am happy to have constructed my first solution in MiniZinc as of today.

Thanks for all your input!

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Guenther