Enigmatic Code

Programming Enigma Puzzles

Enigma 1506: China games

From New Scientist #2668, 9th August 2008

In the three arithmetic additions below, the number 2008 is given and the undisclosed digits have been replaced by letters and smiley faces. Different letters stand for different digits (the same letter consistently stands for the same digit), each face can be any digit, and leading digits cannot be zero.

Find the value of GAMES.

[enigma1506]

2 responses to “Enigma 1506: China games

  1. Jim Randell 9 October 2012 at 10:54 am

    The following Python program runs in 80ms.

    from itertools import permutations
    from enigma import irange, nconcat, printf
    
    # possible digits
    ds0 = set(irange(0, 9))
    
    # from the first sum it follows (ES + IN + NA + IN) % 100 = 8
    for (A, E, I, N, S) in permutations(ds0, 5):
      if (10 * E + S + 20 * I + 12 * N + A) % 100 != 8: continue
    
      # try the remaining digits
      ds1 = ds0.difference((A, E, I, N, S))
      for (C, G, H, M) in permutations(ds1, 4):
        # no leading zeros
        if 0 in (C, G, I): continue
    
        # from sum 3 it follows C + G + I + 1 + (2 + H + A + N)/10 < 10
        if C + G + I + (2 + H + A + N) // 10 > 8: continue
        
        GAMES = nconcat(G, A, M, E, S)
        IN = nconcat(I, N)
        CHINA = nconcat(C, H, I, N, A)
    
        # sum 2:
        # a2008 + GAMES + INbcd = CHINA
        a0bcd = CHINA - (2008 + GAMES + IN * 1000)
        if not(9999 < a0bcd < 100000): continue
        s = str(a0bcd)
        if s[1] != '0': continue
    
        # sum 3:
        # e2008 + CHINA + GAMES + INfgh = i2008
        # (2008 + HINA + AMES + Nfgh) % 10000 = 2008
        Nfgh = (2008 - (2008 + CHINA % 10000 + GAMES % 10000) % 10000) % 10000
        (n, fgh) = divmod(Nfgh, 1000)
        if n != N: continue
        for e in irange(1, 9):
          i2008 = e * 10000 + 2008 + CHINA + GAMES + IN * 1000 + fgh
          (i, r) = divmod(i2008, 10000)
          if not(0 < i < 10 and r == 2008): continue
    
          # sum 1:
          # GAMES + xxxIN + CHINA + yyyIN = z2008
          # where 99 < xxx, yyy < 1000
          for z in irange(1, 9):
            (xy, r) = divmod(z * 10000 + 2008 - (GAMES + 2 * IN + CHINA), 100)
            if not(199 < xy < 1999 and r == 0): continue
    
            printf("[1] {GAMES} + xxx{IN} + {CHINA} + yyy{IN} = {z}2008, xxx + yyy = {xy}")
            printf("[2] {a}2008 + {GAMES} + {IN}{bcd} = {CHINA}", a=s[0], bcd=s[2:])
            printf("[3] {e}2008 + {CHINA} + {GAMES} + {IN}{fgh} = {i}2008")
    
            printf("GAMES={GAMES} IN={IN} CHINA={CHINA}")
    

    Solution: GAMES = 23785.

  2. geoffrounce 9 August 2017 at 9:06 am
    % A Solution in MiniZinc
    % Lower case letters represent smiley faces
    
    %       GAMES   h2008   m2008
    %       abcIN   GAMES   CHINA
    %       CHINA   INijk   GAMES
    %       defIN   -----   INnpq
    %       -----   CHINA   ----- 
    %       g2008   -----   r2008
    %       -----           -----
    
    include "globals.mzn";
     
    var 0..9:G;  var 0..9:A;  var 0..9:M;  var 0..9:E;  var 0..9:S;  
    var 0..9:I;  var 0..9:N;  var 0..9:C;  var 0..9:H; 
    
    constraint all_different([G, A, M, E, S, I, N, C, H]);  
    
    var 0..9:a;  var 0..9:b;  var 0..9:c;  var 0..9:d;
    var 0..9:e;  var 0..9:f;  var 0..9:g;  var 0..9:h;
    var 0..9:i;  var 0..9:j;  var 0..9:k;  var 0..9:m;
    var 0..9:n;  var 0..9:p;  var 0..9:q;  var 0..9:r;
    
    constraint G != 0 /\ a != 0 /\ C != 0 /\ d != 0 /\ h != 0
    /\ I != 0 /\ m != 0 /\ r != 0;
    
    var 10000..99999:GAMES = 10000*G + 1000*A + 100*M + 10*E + S;
    var 10000..99999:abcIN = 10000*a + 1000*b + 100*c + 10*I + N;
    var 10000..99999:CHINA = 10000*C + 1000*H + 100*I + 10*N + A;
    var 10000..99999:defIN = 10000*d + 1000*e + 100*f + 10*I + N;
    var 10000..99999:g2008 = g*10000 + 2008;
    var 10000..99999:h2008 = h*10000 + 2008;
    var 10000..99999:INijk = 10000*I + 1000*N + 100*i + 10*j + k;
    var 10000..99999:m2008 = m*10000 + 2008;
    var 10000..99999:INnpq = 10000*I + 1000*N + 100*n + 10*p + q;
    var 10000..99999:r2008 = r*10000 + 2008;
     
    constraint GAMES + abcIN + CHINA + defIN = g2008;
    
    constraint h2008 + GAMES + INijk = CHINA;
    
    constraint m2008 + CHINA + GAMES + INnpq = r2008;
    
    solve satisfy;
    
    output [ "GAMES = " ++ show(GAMES) ++ "\n" ] ++
           [ show(GAMES) ++ " + " ++ show(abcIN) ++ " + " ++ show(CHINA) ++ 
           " + " ++ show(defIN) ++ " = " ++ show(g2008) ++ "\n"] 
           
      ++   [ show(h2008) ++ " + " ++ show(GAMES) ++ " + " ++ show(INijk) ++ 
      " = " ++ show(CHINA) ++ "\n"]
      
      ++   [ show(m2008) ++ " + " ++ show(CHINA) ++ " + " ++ show(GAMES) ++ 
      " + " ++ show(INnpq) ++ " = " ++ show(r2008) ];
    
    % GAMES = 23785
    % 23785 + 11910 + 46103 + 10210 = 92008
    % 12008 + 23785 + 10310 = 46103
    % 12008 + 46103 + 23785 + 10112 = 92008
    % ------------------
    % Finished in 58msec
    

    Allowing for numbers containing smiley faces to take different values, I found 22 solutions to the three equations in multiple output configuration for MiniZinc.

    However, in all case, GAMES = 23785, IN = 10, CHINA = 46103 and the totals of the first and third equations was 92008.

Leave a reply to geoffrounce Cancel reply

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