Enigmatic Code

Programming Enigma Puzzles

Enigma 405: Uncle bungles the answer

From New Scientist #1555, 9th April 1987 [link]

It is true, of course, that there are rather a lot of letters in this puzzle, but despite that I though that for once Uncle Bungle was going to write it out correctly. In fact there was no mistake until the answer but in that, I’m afraid, one of the letters was incorrect.

This is another addition sum with letters substituted for digits. Each letter stands for the same digit whenever it appears, and different letters stand for different digits. Or at least they should, and they do, but for the mistake in the last line across.

Enigma 405

Which letter is wrong?

Write out the correct addition sum.

Note: This is a corrected version of Enigma 401.

[enigma405]

Advertisements

One response to “Enigma 405: Uncle bungles the answer

  1. Jim Randell 14 July 2017 at 8:55 am

    This puzzle is a corrected version of Enigma 401. In the original puzzle the result of the addition sum was given incorrectly.

    As noted in my comment on Enigma 1113 I have used several methods in the past to solve this kind of problem.

    Here is a Python program that uses the SubstitutedSum() solver from the enigma.py library. It runs in 503ms.

    from enigma import SubstitutedSum, join, printf
    
    # the incorrect alphametic sum: X + Y = Z
    (X, Y, Z) = ("YTBBEDMKD", "YHDBTYYDD", "EDYTERTPTY")
    
    # the symbols used (maybe plus an extra one)
    symbols = join(sorted(set(X + Y + Z)))
    if len(symbols) < 10: symbols += '?'
    
    # one of the letters in Z is wrong
    for (i, z) in enumerate(Z):
      # choose a replacement symbol
      for r in symbols:
        if r == z: continue
        # create a solver for the modified sum
        p = SubstitutedSum([X, Y], Z[:i] + r + Z[i + 1:])
        # and solve it
        for s in p.solve():
          printf("pos {i}, {z}->{r} / {p.text} / {t}", t=p.substitute(s, p.text))
    

    And here is Python program that uses the minizinc.py wrapper to generate a MiniZinc model to find the solution. It runs in 233ms.

    from enigma import substitute, join, printf
    from minizinc import MiniZinc, var, alphametic
    
    # make the MiniZinc model
    p = MiniZinc(f"""
    
    include "globals.mzn";
    
    % the symbols in the incorrect sum
    {var("0..9", "BDEHKMPRTY")};
    
    constraint all_different([B, D, E, H, K, M, P, R, T, Y]);
    
    % make 10 new symbols for the correct answer
    {var("0..9", "abcdefghij")};
    
    % the new sum is a correct alphametic sum
    constraint {alphametic("{YTBBEDMKD} + {YHDBTYYDD} = {abcdefghij}")};
    
    % no leading zeros
    constraint Y != 0 /\ a != 0;
    
    % the new answer is different from the original answer in only one place
    constraint sum([a != E, b != D, c != Y, d != T, e != E, f != R, g != T, h != P, i != T, j != Y]) = 1;
    
    solve satisfy;
    
    """)
    
    # solve the model
    for s in p.solve(solver="mzn-g12fd -a", verbose=0):
      # map of digits to letters
      d = dict((s[k], k) for k in "BDEHKMPRTY")
      # find the mismatched letters in the answer
      for (i, (a, b)) in enumerate(zip("EDYTERTPTY", "abcdefghij")):
        if s[a] != s[b]:
          printf("position {i} is incorrect [{a} should be {b}]", b=d.get(s[b], '?'))
      # print the actual sum
      printf("sum: {s}", s=substitute(s, "YTBBEDMKD + YHDBTYYDD = abcdefghij"));
    

    Solution: The 9th (penultimate) letter of the result is wrong. (The T should be an H). The correct sum should be: 695513243 + 673596633 = 1369109876.

    So, the puzzle that Uncle Bungle should have submitted is:

    YTBBEDMKD + YHDBTYYDD = EDYTERTPHY

    Which can be solved directly using the SubstitutedSum solver from the enigma.py library in 120ms:

    % python -m enigma SubstitutedSum "YTBBEDMKD + YHDBTYYDD = EDYTERTPHY"
    (YTBBEDMKD + YHDBTYYDD = EDYTERTPHY)
    (695513243 + 673596633 = 1369109876) / B=5 D=3 E=1 H=7 K=4 M=2 P=8 R=0 T=9 Y=6
    

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: