Enigmatic Code

Programming Enigma Puzzles

Enigma 366: On the scent

From New Scientist #1515, 3rd July 1986 [link]

In this puzzle you scientists are on the scent for some facts and, as is often the case, different digits have been consistently replaced by different letters.

SCENT is a factor of SCIENTIST

SENT = F × ACT

(and, in fact, F also goes into SCENT, namely S thousand more times than it goes into SENT.)

What’s the SENSE?

[enigma366]

Advertisements

2 responses to “Enigma 366: On the scent

  1. Jim Randell 14 October 2016 at 7:06 am

    This Python program uses the SubstitutedSum() solver from the enigma.py library. It runs in 47ms.

    from enigma import irange, SubstitutedSum, printf
    
    digits = set(irange(0, 9))
    
    # choose a digit for F
    for F in digits:
    
      # solve SENT = F x ACT
      s = SubstitutedSum(['ACT'] * F, 'SENT', l2d={ 'F': F })
      for r in s.solve():
    
        # check SCENT = F x SACT
        (SCENT, SACT) = (int(s.substitute(r, x)) for x in ('SCENT', 'SACT'))
        if SCENT != F * SACT: continue
    
        # choose an unused digit for I
        for I in digits.difference(r.values()):
    
          # determine SCIENTIST
          r['I'] = I
          SCIENTIST = int(s.substitute(r, 'SCIENTIST'))
          if SCIENTIST % SCENT > 0: continue
    
          # print the answer
          SENSE = s.substitute(r, 'SENSE')
          printf("SENSE={SENSE} [{r}]", r=', '.join(k + '=' + str(r[k]) for k in sorted(r.keys())))
    

    Solution: SENSE = 27427.

    Or we can use the general alphametic solver (SubstitutedExpression()) from the enigma.py library, and solve the problem from the command line without needing to write a program at all.

    Here’s the command and its output:

    % python -m enigma SubstitutedExpression "SCIENTIST % SCENT = 0" "F * ACT = SENT" "F * SACT = SCENT"
    (SCIENTIST % SCENT = 0) (F * ACT = SENT) (F * SACT = SCENT)
    (201745125 % 20745 = 0) (9 * 305 = 2745) (9 * 2305 = 20745) / A=3 C=0 E=7 F=9 I=1 N=4 S=2 T=5 / 27427
    SENSE = 27427 [1 solution]
    

    This runs in 77ms.

  2. Brian Gladman 14 October 2016 at 10:10 am
    from  itertools import permutations
    from functools import reduce
    
    # SENT = F * ACT and SCENT = F * SACT --> SENT = (9 + C / S) * ACT
    for A, C, S, T in permutations(range(10), 4):
      if not A or not S or C % S:
        continue
      ACT = 100 * A + 10 * C + T
      
      # form SENT and extract its digits (checking any already defined) 
      SENT = (9 + C // S) * ACT
      if not 1000 < SENT < 10000:
        continue
      s, E, N, t = (int(x) for x in str(SENT))
      if s != S or t != T or E == N or {E, N} & {A, C, S, T}:
        continue
      
      # extract F using F = SENT / ACT
      F, r = divmod(SENT, ACT)
      if r or not 0 < F < 10:
        continue
    
      SCENT = 1000 * (9 * S + C) + SENT 
      # now consider values for the remaining letter I
      for I in set(range(10)).difference([A, C, E, F, N, S, T]):
        
        # SCIENTIST is divisible by SCENT
        tu = (S, C, I, E, N, T, I, S, T)
        SCIENTIST = reduce(lambda x, y: 10 * x + y, tu)
        if not SCIENTIST % SCENT:
          
          SENSE = reduce(lambda x, y: 10 * x + y, (S, E, N, S, E))
          fs = 'A:{}, C:{}, E:{}, F:{}, I:{}, N:{}, S:{}, T:{}'
          s = fs.format(A, C, E, F, I, N, S, T)
          print('SENSE = {} ({})'.format(SENSE, s))
    

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: