# 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]

### One response 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):
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.