Enigmatic Code

Programming Enigma Puzzles

Enigma 336: Seven teams, two leagues

From New Scientist #1484, 28th November 1985 [link]

Three football teams (A, B and C) are to play each other once, and four other football teams (D, E, F, and G) are also to play each other once. After some — or perhaps all — of the matches had been played, two separate tables were drawn up giving some details of the games played, won, lost etc.

But in these two tables, just to make things more interesting, letters have been substituted for digits. In both tables the same letter stands for the same digit (from 0 to 9) whenever it appears, and different letters stand for different digits.

The tables looked like this:

Enigma 336

(Two points are given for a win, and one point to each side in a drawn match).

Find the score in each match.



One response to “Enigma 336: Seven teams, two leagues

  1. Jim Randell 11 March 2016 at 6:34 am

    This Python program uses the Football() helper class from the enigma.py library, even so it is a bit tedious. It runs in 42ms.

    from enigma import irange, Football, printf
    # possible single digits
    digits = set(irange(0, 9))
    # set the scoring system
    football = Football(points={ 'w': 2, 'd': 1 })
    # choose the outcomes for the A's matches
    for (AB, AC) in football.games(repeat=2):
      # compute the table for A
      A = football.table([AB, AC], [0, 0])
      (h, p, m) = (A.w, A.l, A.points)
      if len(set((h, p, m))) != 3: continue
      # and the remaining match
      for BC in football.games():
        # compute the table for B
        B = football.table([AB, BC], [1, 0])
        if not(p == B.d == B.points): continue
        # compute the table for C
        C = football.table([AC, BC], [1, 1])
        if not(m == C.played): continue
        # and now consider the outcomes for D
        for (DE, DF, DG) in football.games(repeat=3):
          # compute the table for D
          D = football.table([DE, DF, DG], [0, 0, 0])
          if not(D.points == h): continue
          # and the remaining matches for G
          for (EG, FG) in football.games(repeat=2):
            # compute the table for G
            G = football.table([DG, EG, FG], [1, 1, 1])
            if not(G.played == h): continue
            # and the remaining match
            for EF in football.games():
              # compute the table for E
              E = football.table([DE, EF, EG], [1, 0, 0])
              if not(E.d == p): continue
              # compute the table for F
              F = football.table([DF, EF, FG], [1, 1, 0])
              r = F.played
              if r in (h, m, p): continue
              k = F.points
              if k in (h, m, p, r): continue
              # possible scorelines for E
              for (sDE, sEF, sEG) in football.scores([DE, EF, EG], [1, 0, 0], h, h):
                # possible scorelines for G
                for (sDG, sFG) in football.scores([DG, FG], [1, 1], r, k, [sEG], [1]):
                  # possible scorelines for A
                  for (sAB, sAC) in football.scores([AB, AC], [0, 0], k, r):
                    # consider possible values for t
                    for t in digits.difference([h, k, m, p, r]):
                      # and possible scorelines for B
                      for (sBC,) in football.scores([BC], [0], t, k, [sAB], [1]):
                        # and check the for/against scores for C
                        (j, y) = football.goals([sAC, sBC], [1, 1])
                        if len(digits.difference([h, j, k, m, p, r, t, y])) != 2: continue
                        # consider possible goals against F
                        for aF in irange(0, 9):
                          # the scoreline for the remaining match
                          for (sDF,) in football.scores([DF], [1], y, aF, [sEF, sFG], [1, 0]):
                            printf("h={h} k={k} m={m} p={p} r={r} t={t}")
                            printf("AB={AB}:{sAB} AC={AC}:{sAC} BC={BC}:{sBC}")
                            printf("DE={DE}:{sDE} DF={DF}:{sDF} DG={DG}:{sDG} EF={EF}:{sEF} EG={EG}:{sEG} FG={FG}:{sFG}")
                            printf("A={A} fA={k} aA={r}")
                            printf("B={B} fB={t} aB={k}")
                            printf("C={C} fC={j} aC={y}")
                            (fD, aD) = football.goals([sDE, sDF, sDG], [0, 0, 0])
                            printf("D={D} fD={fD} aD={aD}")
                            printf("E={E} fE={h} aE={h}")
                            printf("F={F} fF={y} aF={aF}")
                            printf("G={G} fG={r} aG={k}")

    Solution: The scores in the matches played so far are: A vs. C = 5-3; B vs. C = 4-5; D vs. E = 0-1; D vs. F = 3-3; E vs. F = 0-1; F vs. G = 5-3.

    The following matches are not yet played: A vs. B, D vs. G, E vs. G.

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: