Enigmatic Code

Programming Enigma Puzzles

Enigma 393: Decode the sum

From New Scientist #1543, 15th January 1987 [link]

In the following addition sum, different letters stand for different digits and the same letter stands for the same digit throughout.

Decode the sum.

[enigma393]

Advertisements

3 responses to “Enigma 393: Decode the sum

  1. Jim Randell 28 April 2017 at 8:09 am

    New Scientist published two substituted addition sums in a row (see Enigma 392).

    The SubstitutedSum() solver from the enigma.py library makes short work of this problem too. We can run it directly from the command line. Here’s the relevant command and output. It executes in 142ms.

    % python -m enigma SubstitutedSum "ABCDEFGHIJ + DGEHIBJFCA = HJDFIGEACB"                        
    (ABCDEFGHIJ + DGEHIBJFCA = HJDFIGEACB)
    (4721985603 + 1596073824 = 6318059427) / A=4 B=7 C=2 D=1 E=9 F=8 G=5 H=6 I=0 J=3
    

    Solution: The correct sum is: 4721985603 + 1596073824 = 6318059427.

  2. geoffrounce 28 April 2017 at 9:41 am

    Yes, longer to write a programme in MiniZinc – I tried to make all the zeroes a bit more readable by using the “pow” function.

    What would your solver look like if it was run as a programme under the Idle interface ?
    Is there a reason why you prefer to run your solver from the command line?

    % A Solution in MiniZinc
    include "globals.mzn";
    
    var 1..9:A;  var 0..9:B;  var 0..9:C;  var 1..9:D; 
    var 0..9:E;  var 0..9:F;  var 0..9:G;  var 1..9:H; 
    var 0..9:I;  var 0..9:J;
    
    constraint alldifferent([A,B,C,D,E,F,G,H,I,J]);
    
    var 1000000000..9999999999: ABCDEFGHIJ = A*pow(10,9) + B*pow(10,8) + C*pow(10,7)
    + D*pow(10,6) + E*pow(10,5) + F*pow(10,4) + G*pow(10,3) + H*pow(10,2) + 10*I + J;
    
    var 1000000000..9999999999: DGEHIBJFCA = D*pow(10,9) + G*pow(10,8) + E*pow(10,7)
    + H*pow(10,6) + I*pow(10,5) + B*pow(10,4) + J*pow(10,3) + F*pow(10,2) + 10*C + A;
    
    var 1000000000..9999999999: HJDFIGEACB = H*pow(10,9) + J*pow(10,8) + D*pow(10,7) 
    + F*pow(10,6) + I*pow(10,5) + G*pow(10,4) + E*pow(10,3) + A*pow(10,2) + 10*C + B;
    
    constraint ABCDEFGHIJ + DGEHIBJFCA == HJDFIGEACB;
    
    solve satisfy;
    
    output[ "Correct sum is : " ++show(ABCDEFGHIJ) ++ " + " ++ show(DGEHIBJFCA) ++ 
    " = " ++ show(HJDFIGEACB) ];
    
    % Correct sum is : 4721985603 + 1596073824 = 6318059427
    % Finished in 130msec
    
    
    • Jim Randell 28 April 2017 at 10:55 pm

      @geoff: To answer your questions…

      I don’t use IDLE myself. I run the programs I write using the command line, which is why I find it most convenient to solve simple problems directly from the command line, rather than write a program.

      But I’ve made the code that is used to invoke a solver from the command line available as the run() function in the enigma.py, so that you can call it from an interactive Python session (like the one IDLE gives you).

      So the equivalent of typing this at the command line:

      % python -m enigma "<arg_1>" "<arg_2>" ... "<arg_n>"
      

      is typing this at an interactive Python prompt:

      >>> import enigma
      >>> enigma.run("<arg_1>", "<arg_2>", ..., "<arg_n>")
      

      So for this problem we can invoke the SubstitutedSum() solver in the enigma.py library as follows:

      >>> import enigma
      >>> enigma.run("SubstitutedSum", "ABCDEFGHIJ + DGEHIBJFCA = HJDFIGEACB")
      (ABCDEFGHIJ + DGEHIBJFCA = HJDFIGEACB)
      (4721985603 + 1596073824 = 6318059427) / A=4 B=7 C=2 D=1 E=9 F=8 G=5 H=6 I=0 J=3
      

      Or you can place the arguments in a run file using a “shell-like” syntax (it is parsed using the Python standard shlex library [ https://docs.python.org/3/library/shlex.html ]).

      So, for example, if we create a file called enigma393.run with the following line in it:

      SubstitutedSum "ABCDEFGHIJ + DGEHIBJFCA = HJDFIGEACB"
      

      Then we can invoke it from an interactive Python session as follows.

      >>> import enigma
      >>> enigma.run("enigma393.run")
      (ABCDEFGHIJ + DGEHIBJFCA = HJDFIGEACB)
      (4721985603 + 1596073824 = 6318059427) / A=4 B=7 C=2 D=1 E=9 F=8 G=5 H=6 I=0 J=3
      

      Which is the same as invoking it directly from the command line:

      % python -m enigma -r enigma393.run
      (ABCDEFGHIJ + DGEHIBJFCA = HJDFIGEACB)
      (4721985603 + 1596073824 = 6318059427) / A=4 B=7 C=2 D=1 E=9 F=8 G=5 H=6 I=0 J=3
      

      (The -r is optional in this case).

      I actually create run files, even for simple problems like this, so I have a record of my solution that I can execute directly.

      Of course, if you are at an interactive Python prompt, you can just invoke the solver directly as a Python expression. In this case we can use:

      >>> from enigma import SubstitutedSum
      >>> SubstitutedSum(["ABCDEFGHIJ", "DGEHIBJFCA"], "HJDFIGEACB").go()
      ABCDEFGHIJ + DGEHIBJFCA = HJDFIGEACB
      4721985603 + 1596073824 = 6318059427 / A=4 B=7 C=2 D=1 E=9 F=8 G=5 H=6 I=0 J=3
      

      The same two lines could be placed into a file called enigma393.py and executed as a Python program.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: