Enigmatic Code

Programming Enigma Puzzles

Enigma 1515: Relative sizes

From New Scientist #2677, 11th October 2008

 In the following statements, whole numbers are expressed in words written in capital letters, where different letters stand for different digits, the same letter consistently stands for the same digit, and leading digits cannot be zero.

If the total of four BEES and a SNAIL is greater than the total of eight FLEAS and an ANT; and if the total of 18 ANTS and a GNAT is greater than the total of 18 FLIES and a FLEA, find the value of BEETLES.

[enigma1515]

Advertisements

3 responses to “Enigma 1515: Relative sizes

  1. Jim Randell 24 September 2012 at 11:30 am

    The following Python program runs in 1.8s (using PyPy).

    from functools import reduce
    from itertools import permutations
    from enigma import irange, printf
    
    def nconcat(*args):
      return reduce(lambda a, b: a * 10 + b, args, 0)
    
    # leading digits
    ds = set(irange(1, 9))
    for (A, B, F, G, S) in permutations(ds, 5):
      ds0 = ds.difference((A, B, F, G, S)).union([0])
      for (E, I, L, N, T) in permutations(ds0):
        BEES = nconcat(B, E, E, S)
        SNAIL = nconcat(S, N, A, I, L)
        FLEA = nconcat(F, L, E, A)
        FLEAS = nconcat(FLEA, S)
        ANT = nconcat(A, N, T)
        if not(4 * BEES + SNAIL > 8 * FLEAS + ANT): continue
    
        ANTS = nconcat(ANT, S)
        GNAT = nconcat(G, N, A, T)
        FLIES = nconcat(F, L, I, E, S)
        if not(18 * ANTS + GNAT > 18 * FLIES + FLEA): continue
    
        BEETLES = nconcat(B, E, E, T, L, E, S)
        printf("BEETLES = {BEETLES} [A={A} B={B} E={E} F={F} G={G} I={I} L={L} N={N} S={S} T={T}]")
    

    Solution: BEETLES = 4335036.

    • Naim Uygun 24 September 2012 at 12:18 pm
      """
      Answer:   BEETLES=4335036
      In this enigma there are two inequalities.
      If we add them side by side, there will be a lot of BEETLES.
      """
      from itertools import permutations
      for p in permutations("1234567890",10):
          b=int(p[0])
          if b==0: continue
          e=int(p[1])
          s=int(p[2])
          if s==0: continue
          n=int(p[3])
          a=int(p[4])
          if a==0 : continue
          i=int(p[5])
          l=int(p[6])
          t=int(p[7])
          g=int(p[8])
          if g==0: continue
          f=int(p[9])
          if f==0: continue
          bees=1000*b+100*e+10*e+s
          snail=10000*s+1000*n+100*a+10*i+l
          ants=1000*a+100*n+10*t+s
          gnat=1000*g+100*n+10*a+t
          fleas=10000*f+1000*l+100*e+10*a+s
          ant=100*a+10*n+t
          flies=10000*f+1000*l+100*i+10*e+s
          flea=1000*f+100*l+10*e+a
          if 4*bees+snail<=8*fleas+ant : continue
          if 18*ants+gnat <= 18*flies+flea : continue
          print(b,e,e,t,l,e,s)
      
    • Jim Randell 27 October 2016 at 11:04 pm

      Using the generalised alphametic solver (SubstitutedExpression()) from the enigma.py library lets us get the solution directly from the command line.

      % pypy -m enigma SubstitutedExpression --answer="BEETLES" --reorder=0 "18 * ANTS + GNAT > 18 * FLIES + FLEA" "4 * BEES + SNAIL > 8 * FLEAS + ANT"
      (4 * BEES + SNAIL > 8 * FLEAS + ANT) (18 * ANTS + GNAT > 18 * FLIES + FLEA)
      (4 * 4336 + 68920 > 8 * 10396 + 985) (18 * 9856 + 7895 > 18 * 10236 + 1039) / A=9 B=4 E=3 F=1 G=7 I=2 L=0 N=8 S=6 T=5 / 4335036
      BEETLES = 4335036 [1 solution]
      

      Run time is 729ms, so it also runs faster than the program I gave above.

      Or we can put all the arguments into a file and pass that to the enigma.py library:

      #!/usr/bin/env pypy -m enigma -r
      
      SubstitutedExpression
      
      --answer="BEETLES"
      --reorder=0
      
      "18 * ANTS + GNAT > 18 * FLIES + FLEA"
      "4 * BEES + SNAIL > 8 * FLEAS + ANT"
      

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: