Enigmatic Code

Programming Enigma Puzzles

Enigma 1133: Smile

From New Scientist #2289, 5th May 2001 [link]

enigma-1133

In the given multiplication (which contains no zeros), different letters stand for different digits but the same letter always stands for the same digit and a smiley face of course, can be any digit.

If YES is odd, how big is your SMILE?

[enigma1133]

Advertisements

One response to “Enigma 1133: Smile

  1. Jim Randell 9 January 2017 at 10:03 am

    I wrote code to maximise the possible value for SMILE, but it turns out there is only one possible sum (even if YES is not restricted to be odd, provided zero digits are not used).

    This Python program uses the alphametic solver (SubstitutedExpression()) from the enigma.py library. It runs in 70ms.

    from enigma import SubstitutedExpression, Accumulator, irange, printf
    
    # maximise SMILE
    r = Accumulator(fn=max)
    
    # use the SubstitutedExpression solver
    p = SubstitutedExpression([
        "YES * ABC = SMILE",
        "C * YES = PQR",
        "B * YES = UVW",
        "A * YES = YES"
      ],
      digits=irange(1, 9),
      distinct="SMILEY",
      d2i={ 2: 'S', 4: 'S', 6: 'S', 8: 'S' },
    )
    
    # solve the puzzle
    for d in p.solve(verbose=1):
      r.accumulate(int(p.substitute(d, "SMILE")))
    
    printf("max(SMILE) = {r.value}")
    

    Solution: SMILE = 34716.

    If we do without the code to maximise SMILE, we don’t have to write a program at all:

    % python -m enigma SubstitutedExpression --digits=1-9 --distinct=SMILEY --invalid=2468,S --answer=SMILE "YES * ABC = SMILE" "C * YES = PQR" "B * YES = UVW" "A * YES = YES"
    (YES * ABC = SMILE) (C * YES = PQR) (B * YES = UVW) (A * YES = YES)
    (263 * 132 = 34716) (2 * 263 = 526) (3 * 263 = 789) (1 * 263 = 263) / A=1 B=3 C=2 E=6 I=7 L=1 M=4 P=5 Q=2 R=6 S=3 U=7 V=8 W=9 Y=2 / 34716
    SMILE = 34716 [1 solution]
    

    Or the command line arguments can be placed in run-file:

    #!/usr/bin/env python enigma.py -r
    
    # solver to use
    SubstitutedExpression
    
    # solver parameters
    --digits=1-9
    --distinct="SMILEY"
    --invalid="2468,S"
    --answer="SMILE"
    
    # expressions to solve
    "YES * ABC = SMILE"
    "C * YES = PQR"
    "B * YES = UVW"
    "A * YES = YES"
    

    I also wrote a custom program that uses itertools.permutations(). It solves the Alphametic problem in 55ms, which is only slightly faster than using SubstitutedExpression().

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: