1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/crypto.mod Mon Dec 06 13:09:21 2010 +0100
1.3 @@ -0,0 +1,84 @@
1.4 +/* CRYPTO, a crypto-arithmetic puzzle */
1.5 +
1.6 +/* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
1.7 +
1.8 +/* This problem comes from the newsgroup rec.puzzle.
1.9 + The numbers from 1 to 26 are assigned to the letters of the alphabet.
1.10 + The numbers beside each word are the total of the values assigned to
1.11 + the letters in the word (e.g. for LYRE: L, Y, R, E might be to equal
1.12 + 5, 9, 20 and 13, or any other combination that add up to 47).
1.13 + Find the value of each letter under the equations:
1.14 +
1.15 + BALLET 45 GLEE 66 POLKA 59 SONG 61
1.16 + CELLO 43 JAZZ 58 QUARTET 50 SOPRANO 82
1.17 + CONCERT 74 LYRE 47 SAXOPHONE 134 THEME 72
1.18 + FLUTE 30 OBOE 53 SCALE 51 VIOLIN 100
1.19 + FUGUE 50 OPERA 65 SOLO 37 WALTZ 34
1.20 +
1.21 + Solution:
1.22 + A, B,C, D, E,F, G, H, I, J, K,L,M, N, O, P,Q, R, S,T,U, V,W, X, Y, Z
1.23 + 5,13,9,16,20,4,24,21,25,17,23,2,8,12,10,19,7,11,15,3,1,26,6,22,14,18
1.24 +
1.25 + Reference:
1.26 + Koalog Constraint Solver <http://www.koalog.com/php/jcs.php>,
1.27 + Simple problems, the crypto-arithmetic puzzle ALPHACIPHER. */
1.28 +
1.29 +set LETTERS :=
1.30 +{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
1.31 + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
1.32 +};
1.33 +/* set of letters */
1.34 +
1.35 +set VALUES := 1..card(LETTERS);
1.36 +/* set of values assigned to the letters */
1.37 +
1.38 +set WORDS;
1.39 +/* set of words */
1.40 +
1.41 +param total{word in WORDS};
1.42 +/* total[word] is the total of the values assigned to the letters in
1.43 + the word */
1.44 +
1.45 +var x{i in LETTERS, j in VALUES}, binary;
1.46 +/* x[i,j] = 1 means that letter i is assigned value j */
1.47 +
1.48 +s.t. phi{i in LETTERS}: sum{j in VALUES} x[i,j] = 1;
1.49 +
1.50 +s.t. psi{j in VALUES}: sum{i in LETTERS} x[i,j] = 1;
1.51 +
1.52 +s.t. eqn{word in WORDS}: sum{k in 1..length(word), j in VALUES}
1.53 + j * x[substr(word,k,1), j] = total[word];
1.54 +
1.55 +solve;
1.56 +
1.57 +printf{i in LETTERS} " %s", i;
1.58 +printf "\n";
1.59 +
1.60 +printf{i in LETTERS} " %2d", sum{j in VALUES} j * x[i,j];
1.61 +printf "\n";
1.62 +
1.63 +data;
1.64 +
1.65 +param : WORDS : total :=
1.66 + BALLET 45
1.67 + CELLO 43
1.68 + CONCERT 74
1.69 + FLUTE 30
1.70 + FUGUE 50
1.71 + GLEE 66
1.72 + JAZZ 58
1.73 + LYRE 47
1.74 + OBOE 53
1.75 + OPERA 65
1.76 + POLKA 59
1.77 + QUARTET 50
1.78 + SAXOPHONE 134
1.79 + SCALE 51
1.80 + SOLO 37
1.81 + SONG 61
1.82 + SOPRANO 82
1.83 + THEME 72
1.84 + VIOLIN 100
1.85 + WALTZ 34 ;
1.86 +
1.87 +end;