Debian Codegraph @

tests/spec/unit/regression/enigma_1568.mzn

raw
1 /***
2 !Test
3 solvers: [gecode]
4 expected:
5 - !Result
6 solution: !Solution
7 D: 6
8 E: 5
9 L: 3
10 O: 9
11 ODD: 966
12 P: 1
13 PUZZLE: 102235
14 U: 0
15 Z: 2
16 num1: 161
17 num2: 635
18 num3: 805
19 num4: 483
20 x: [9, 6, 1, 0, 2, 3, 5]
21 ***/
22
23 % Regression test for a bug in mzn2fzn 1.2. The optimisation pass was leaving
24 % dangling references to variables it had "eliminated". The symptom was the
25 % following error from the FlatZinc interpreter:
26 %
27 % enigma_1568.fzn:28:
28 % symbol error: `INT____3' undeclared
29 % enigma_1568.fzn:30:
30 % symbol error: `INT____3' undeclared
31 %
32 % (This model is from the original bug report.)
33
34 % Enigma problem 1568 (Odd puzzle) in MiniZinc.
35 %
36 % From New Scientist
37 % http://www.newscientist.com/article/mg20427311.100-enigma-number-1568.html
38 % """
39 % 21 October 2009 by Albert Haddad
40 %
41 % Odd puzzle
42 %
43 % In this multiplication sum the digits have been replaced by letters and
44 % dots. Different letters stand for different digits, the same letter
45 % stands for the same digit, each dot can be any digit, and leading
46 % digits cannot be zero.
47 %
48 % [
49 % . . . | num1
50 % * . . . | num2
51 % -------
52 % . . . | num3
53 % . . . | num4
54 % O D D | ODD
55 % ---------
56 % P U Z Z L E | PUZZLE
57 %
58 % ]
59 %
60 % What is the six-figure odd PUZZLE?
61 % """
62
63 %
64 % This MiniZinc model was created by Hakan Kjellerstrand, [email protected]
65 % See also my MiniZinc page: http://www.hakank.org/minizinc
66 %
67 include "globals.mzn";
68
69 int: n = 7; % number of unknown
70 set of int: Digits = 0..9;
71 var Digits: O;
72 var Digits: D;
73 var Digits: P;
74 var Digits: U;
75 var Digits: Z;
76 var Digits: L;
77 var Digits: E;
78 array[1..n] of var Digits: x;
79
80 var 100..999: num1;
81 var 100..999: num2;
82 var 100..999: num3;
83 var 100..999: num4;
84 var 100..999: ODD ;
85 var 100000..999999: PUZZLE;
86
87 predicate cp1d(array[int] of var int: x, array[int] of var int: y) =
88 assert(index_set(x) = index_set(y),
89 "cp1d: x and y have different sizes",
90 forall(i in index_set(x)) ( x[i] = y[i] ) )
91 ;
92
93
94 % solve satisfy;
95 solve :: int_search(
96 x ++ [num1,num2,num3,num4,ODD,PUZZLE],
97 first_fail,
98 indomain_min,
99 complete)
100 satisfy;
101
102 constraint
103 cp1d(x, [O,D,P,U,Z,L,E]) /\
104
105 all_different(x) /\
106
107 ODD = 100*O + 10*D + D /\
108 PUZZLE = 100000*P + 10000*U + 1000*Z + 100*Z + 10*L + E /\
109
110 num1 * num2 = num3 + num4*10 + ODD*100 /\
111 PUZZLE = num1 * num2 /\
112
113 PUZZLE mod 2 = 1 /\ % PUZZLE is odd
114
115 % And then code the "long multiplication"
116 num1 * (num2 mod 10) = num3 /\
117 num1 * ((num2 div 10) mod 10) = num4 /\
118 num1 * (num2 div 100) = ODD
119 ;
120
121 output [
122 "O = ", show(O), ";\n",
123 "D = ", show(D), ";\n",
124 "P = ", show(P), ";\n",
125 "U = ", show(U), ";\n",
126 "Z = ", show(Z), ";\n",
127 "L = ", show(L), ";\n",
128 "E = ", show(E), ";\n",
129 "x = ", show(x), ";\n",
130 "num1 = ", show(num1), ";\n",
131 "num2 = ", show(num2), ";\n",
132 "num3 = ", show(num3), ";\n",
133 "num4 = ", show(num4), ";\n",
134 "ODD = ", show(ODD), ";\n",
135 "PUZZLE = ", show(PUZZLE), ";\n"
136 ];