Enigmatic Code

Programming Enigma Puzzles

Enigma 227: Set a tease

From New Scientist #1373, 1st September 1983 [link]

Another letters for numbers problem. It may leave you at sea, for it is a tease to keep you on your toes. You see, each letter stands for a different digit each time it appears.

Enigma 227

What is a TEASE?

Note: I am waiting for a phone line to be connected at my new house, so I only have sporadic access to the internet at the moment. The latest estimate is that I’ll have a connection by the end of October 2014.

[enigma227]

Advertisements

2 responses to “Enigma 227: Set a tease

  1. Jim Randell 6 October 2014 at 7:18 pm

    This Python program uses the SubstitutedDivision() solver from the enigma.py library to examine long division sums with the right “shape”. It runs in 390ms.

    from collections import defaultdict
    from enigma import SubstitutedDivision
    
    # make a sum the right "shape"
    p = SubstitutedDivision(
      '?????', '??', '????',
      [('??', '??', '?'), None, ('???', '???', '?'), ('??', '??', '')]
    )
    
    # make a map from the pairs such that in each (pattern, text) pair
    # the symbols in pattern correspond to different symbols in text
    def accumulate(*pairs):
      m = defaultdict(list)
      for (p, n) in pairs:
        for (k, v) in zip(p, str(n)):
          if v in m[k]: return None
          m[k].append(v)
      return m
    
    for s in p.solve():
      intermediates = p.solution_intermediates(s)
      m = accumulate(
        ('TOES', s.c),
        ('AT', s.b),
        ('TEASE', s.a),
        ('AT', intermediates[0][1]),
        ('SEA', intermediates[1][0]),
        ('SEE', intermediates[1][1]),
        ('AT', intermediates[2][0]),
        ('SO', intermediates[2][1]),
      )
      if m is None: continue
      print(', '.join(k + ' -> ' + ''.join(m[k]) for k in sorted(m.keys())))
      p.solution(s)
    

    Solution: TEASE = 58044.

  2. Jim Randell 18 July 2017 at 9:39 am

    A similar program will work with the new version of the SubstitutedDivision() solver in the enigma.py library, although there are some minor differences (the intermediate subtraction sums are included directly in the returned solutions, but the new version does include empty intermediates, and we can also provide a check() function to the solver, to select the required solution).

    Here is a version of the above program that works with the latest version of the SubstitutedDivision() solver. It runs in 336ms.

    from collections import defaultdict
    from enigma import SubstitutedDivision, join, printf
    
    # make a sum the right "shape"
    p = SubstitutedDivision(
      '????? / ?? = ????',
      ['?? - ?? = ?', None, '??? - ??? =  ?', '?? - ?? = 0']
    )
    
    # make a map from the pairs such that in each (pattern, text) pair
    # the symbols in pattern correspond to different symbols in text
    def accumulate(*pairs):
      m = defaultdict(list)
      for (p, n) in pairs:
        for (k, v) in zip(p, str(n)):
          if v in m[k]: return None
          m[k].append(v)
      return m
    
    # check the solution
    def check(s):
      m = accumulate(
        ('TOES', s.c),
        ('AT', s.b),
        ('TEASE', s.a),
        ('AT', s.subs[0][1]),
        ('SEA', s.subs[2][0]),
        ('SEE', s.subs[2][1]),
        ('AT', s.subs[3][0]),
        ('SO', s.subs[3][1]),
      )
      return (m is not None)
    
    # solve the puzzle
    for s in p.solve(check=check):
      printf("TEASE = {s.a}")
    

    But using the distinct parameter to the new SubstitutedDivision() solver (which is inherited from the SubstitutedExpression() solver) we can specify the collections of symbols that have to take on distinct values amongst their own group. I use lower case letters to stand for the symbols in the division sum, and then group together symbols that correspond to the same upper case letter in the puzzle statement.

    Instead of requiring a specialised program the puzzle can be solved by presenting the right arguments to the SubstitutedDivision() solver. Here is the necessary run file. It executes in 113ms.

    #!/usr/bin/env python -m enigma -r
    
    #          h i j k
    #      -----------
    #  f g ) a b c d e
    #        l m
    #        ---
    #          n p q
    #          r s t
    #          -----
    #              u v
    #              w x
    #              ===
    
    SubstitutedDivision
    
    --symbols="abcdefghijklmnpqrstuvwx"
    # distinct occurrences of A, E, O, S, T
    --distinct="cflqu,bejpst,ix,dknrw,aghmv"
    
    "abcde / fg = hijk"
    
    "ab - lm = n"
    ""
    "npq - rst = u"
    "uv - wx = 0"
    

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: