Enigmatic Code

Programming Enigma Puzzles

Enigma 1361: Enigma Variation

From New Scientist #2520, 8th October 2005

Nimrod is the most famous of Elgar’s Enigma Variations, so it is appropriate that I can write:

Enigma 1361

In this, digits have been consistently replaced by letters, different letters representing different digits, with the letter O representing the digit zero.

What is the 6-digit number represented by ENIGMA?

News

According to my diary, this was the first Enigma puzzle I programmed a solution for when it was published. Subsequently I only did the puzzles sporadically until Enigma 1482, when I started coding solutions every week. Since putting up my solutions on this site I have been working to provide solutions to previous Enigma puzzles that I didn’t solve at the time of publication. Currently there are 342 Enigma puzzles (and solutions) on the site.

[enigma1361]

5 responses to “Enigma 1361: Enigma Variation

  1. Jim Randell 20 February 2013 at 9:11 am

    When it was published I solved this puzzle using a Perl program with 8 nested loops. It ran in 757ms. I recoded it in Python using itertools.permutations(), and that ran in 617ms. But this program uses the SubstitutedSum solver from the enigma.py library and runs in 50ms.

    from enigma import (irange, SubstitutedSum, printf)
    
    w = [ 'ELGAR', 'ENIGMA', 'NIMROD' ]
    p = SubstitutedSum(w[0:2], w[2], digits=irange(1, 9), l2d={'O': 0})
    for s in p.solve():
      v = list(p.substitute(s, x) for x in w)
      printf("{w[0]}={v[0]} {w[1]}={v[1]} {w[2]}={v[2]}")
    

    Solution: ENIGMA = 785463.

    • Naim Uygun 20 February 2013 at 2:04 pm
      # ELGAR + ENIGMA = NIMR0D
      from itertools import permutations
      for e,l,g,a,r,n,i,m,d in permutations("987654321",9):
          elgar=e+l+g+a+r
          enigma=e+n+i+g+m+a
          nimrod=n+i+m+r+'0'+d
           if int(elgar)+int(enigma)==int(nimrod):
              print(elgar,"+",enigma,"=",nimrod)   
      
    • Jim Randell 23 January 2016 at 8:56 am

      Now the enigma.py library includes code to invoke the [[ SubstitutedSum() ]] solver directly from the command line this kind of problem can be solved without the need to write a program at all.

      Run: [ @replit ]

      % python -m enigma SubstitutedSum -aO,0 "ELGAR + ENIGMA = NIMROD"
      (ELGAR + ENIGMA = NIMROD)
      (71439 + 785463 = 856902) / A=3 D=2 E=7 G=4 I=5 L=1 M=6 N=8 O=0 R=9
      

      I thought this was quite fitting as Enigma 1361 was the first Enigma puzzle I wrote a program for.

      • geoffrounce 23 January 2016 at 2:18 pm

        A MiniZinc solution shows why the letter O must be zero – otherwise there would be two more solutions

        include "globals.mzn"; 
        
        var 0..9: e; var 0..9: l; var 0..9: g; var 0..9: a; var 0..9: r;
        var 0..9: n; var 0..9: i; var 0..9: m; var 0..9: o; var 0..9: d;
        
        var 100000..999999: enigma;
        var 10000..99999: elgar;
        var 100000..999999: nimrod;
        
        solve satisfy;
        
        constraint alldifferent([e,l,g,a,r,n,i,m,o,d]) /\ e > 0 /\ n > 0;
        
        constraint
            enigma = (e*100000 + n*10000 + i*1000 + g*100 + m*10 + a) /\
            elgar =  (e*10000 + l*1000 + g*100 + a*10 + r) /\
            nimrod =  (n*100000 + i*10000 + m*1000 + r*100 + o*10 + d) /\
            elgar + enigma == nimrod;
        
        output [show(elgar) ++ " + " ++ show(enigma) ++  " = " ++ show(nimrod)];
        
        % Output
        % ------
        % 57938 + 562903 = 620841
        % ----------
        % 60581 + 673548 = 734129
        % ----------
        % 71439 + 785463 = 856902  << zero must be letter O in enigma
        
        
        • Jim Randell 23 January 2016 at 3:34 pm

          Without the additional condition ELGAR + ENIGMA = NIMROD has three distinct solutions.

          % python -m enigma SubstitutedSum "ELGAR + ENIGMA = NIMROD"
          (ELGAR + ENIGMA = NIMROD)
          (60581 + 673548 = 734129) / A=8 D=9 E=6 G=5 I=3 L=0 M=4 N=7 O=2 R=1
          (57938 + 562903 = 620841) / A=3 D=1 E=5 G=9 I=2 L=7 M=0 N=6 O=4 R=8
          (71439 + 785463 = 856902) / A=3 D=2 E=7 G=4 I=5 L=1 M=6 N=8 O=0 R=9
          

          Giving the value of any one of the letters leads to a unique solution, with the exception of A = 3, which gives two solutions.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.