You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.4 KiB

  1. /* CRYPTO, a crypto-arithmetic puzzle */
  2. /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
  3. /* This problem comes from the newsgroup rec.puzzle.
  4. The numbers from 1 to 26 are assigned to the letters of the alphabet.
  5. The numbers beside each word are the total of the values assigned to
  6. the letters in the word (e.g. for LYRE: L, Y, R, E might be to equal
  7. 5, 9, 20 and 13, or any other combination that add up to 47).
  8. Find the value of each letter under the equations:
  9. BALLET 45 GLEE 66 POLKA 59 SONG 61
  10. CELLO 43 JAZZ 58 QUARTET 50 SOPRANO 82
  11. CONCERT 74 LYRE 47 SAXOPHONE 134 THEME 72
  12. FLUTE 30 OBOE 53 SCALE 51 VIOLIN 100
  13. FUGUE 50 OPERA 65 SOLO 37 WALTZ 34
  14. Solution:
  15. 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
  16. 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
  17. Reference:
  18. Koalog Constraint Solver <http://www.koalog.com/php/jcs.php>,
  19. Simple problems, the crypto-arithmetic puzzle ALPHACIPHER. */
  20. set LETTERS :=
  21. { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  22. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
  23. };
  24. /* set of letters */
  25. set VALUES := 1..card(LETTERS);
  26. /* set of values assigned to the letters */
  27. set WORDS;
  28. /* set of words */
  29. param total{word in WORDS};
  30. /* total[word] is the total of the values assigned to the letters in
  31. the word */
  32. var x{i in LETTERS, j in VALUES}, binary;
  33. /* x[i,j] = 1 means that letter i is assigned value j */
  34. s.t. phi{i in LETTERS}: sum{j in VALUES} x[i,j] = 1;
  35. s.t. psi{j in VALUES}: sum{i in LETTERS} x[i,j] = 1;
  36. s.t. eqn{word in WORDS}: sum{k in 1..length(word), j in VALUES}
  37. j * x[substr(word,k,1), j] = total[word];
  38. solve;
  39. printf{i in LETTERS} " %s", i;
  40. printf "\n";
  41. printf{i in LETTERS} " %2d", sum{j in VALUES} j * x[i,j];
  42. printf "\n";
  43. data;
  44. param : WORDS : total :=
  45. BALLET 45
  46. CELLO 43
  47. CONCERT 74
  48. FLUTE 30
  49. FUGUE 50
  50. GLEE 66
  51. JAZZ 58
  52. LYRE 47
  53. OBOE 53
  54. OPERA 65
  55. POLKA 59
  56. QUARTET 50
  57. SAXOPHONE 134
  58. SCALE 51
  59. SOLO 37
  60. SONG 61
  61. SOPRANO 82
  62. THEME 72
  63. VIOLIN 100
  64. WALTZ 34 ;
  65. end;