Enigmatic Code

Programming Enigma Puzzles

Enigma 1245: It’s a knockout!

From New Scientist #2401, 28th June 2003 [link]

Eight teams entered a knockout football competition. Extra time, where necessary, ensured that there were no draws. Four goals were scored in the final, but fewer than that in each of the other games. The scores in the two semi-final matches were the same. Illustrated below is part of the table of the total goals scored against and for each team.

Enigma 1245

In the entries show, digits have been consistently replaced by letters, different letters being used for different digits.

Which four teams got through to the semi-finals of the competition?

[enigma1245]

Advertisements

3 responses to “Enigma 1245: It’s a knockout!

  1. Jim Randell 28 March 2015 at 8:07 am

    Another football puzzle. I used the Football() helper class from enigma.py to count the goals for/against. This Python program runs in 273ms.

    from itertools import product, permutations
    from collections import Counter
    from enigma import Football, irange, printf
    
    # suppose the knockout competiton proceeds as follows:
    #
    # AvB CvD EvF GvH
    #  A v C   E v G
    #    A   v   E
    #        A
    
    football = Football()
    
    # record the teams who got through to the semis
    r = Counter()
    
    # there were 4 goals in the final
    for AE in ((4, 0), (3, 1)):
      # there are less than 4 goals in the semis, and the scores are the same
      for AC in ((3, 0), (2, 1), (2, 0), (1, 0)):
        EG = AC
        # each quarter final has less than 4 goals scored
        for (AB, CD, EF, GH) in product(((3, 0), (2, 1), (2, 0), (1, 0)), repeat=4):
          # compute goals for/against for each team
          teams = (
            football.goals([AB, AC, AE], [0, 0, 0]),
            football.goals([AB], [1]),
            football.goals([CD, AC], [0, 1]),
            football.goals([CD], [1]),
            football.goals([EF, EG, AE], [0, 0, 1]),
            football.goals([EF], [1]),
            football.goals([GH, EG], [0, 1]),
            football.goals([GH], [1]),
          )
    
          # count the goals against
          ga = Counter(x[1] for x in teams)
          # there should be six different values
          if not(len(ga.keys()) == 6): continue
          # and the occurences should be [2, 2, 1, 1, 1, 1]
          if not(sorted(ga.values(), reverse=True) == [2, 2, 1, 1, 1, 1]): continue
    
          # choose a permutation of teams for the table
          for p in permutations(irange(0, 7)):
            (T1, T2, T3, T4, T5, T6, T7, T8) = p
    
            # T6 = O/O, O occurs twice in goals against
            (O, O1) = teams[T6]
            if not(O == O1 and ga[O] == 2): continue
    
            # T5 = N/K, K occurs twice in goals against, N occurs once
            (N, K) = teams[T5]
            if not(ga[K] == 2 and ga[N] == 1): continue
    
            # T7 = W/U, W does not occur in goals against, U occurs once
            (W, U) = teams[T7]
            if not(ga[W] == 0 and ga[U] == 1): continue
    
            # T1 = ?/K, T2 = ?/N, T3 = ?/O
            if not(teams[T1][1] == K and teams[T2][1] == N and teams[T3][1] == O): continue
    
            # T4 = ?/C, T8 = ?/T
            (C, T) = (teams[T4][1], teams[T8][1])
    
            # check all the letters are different
            if len(set((K, N, O, C, U, T, W))) != 7: continue
    
            # the teams in the semi finals are A (0), C (2), E (4), G (6)
            semis = tuple(i for (i, t) in enumerate(p, start=1) if t in (0, 2, 4, 6))
            r[semis] += 1
    
            printf("[semis={semis} O={O} K={K} N={N} U={U} W={W} C={C} T={T}]")
            printf("[AB={AB} CD={CD} EF={EF} GH={GH} / AC={AC} EG={EG} / AE={AE}]")
            printf("[f/a = {teams}]")
            printf()
    
    # output solutions
    for (k, v) in r.items():
      printf("semis={k} [{v} solutions]")
    

    Solution: Teams 4, 6, 7 and 8 got through to the semi-finals.

    The winner of the competition is Team 7 with 8 goals for and 0 goals against (W=8, U=0).

    The runner-up has 5 goals for and 5 goals against. It can either be Team 4 (C=5) or Team 8 (T=5).

    Of the teams that lost in the semi-finals had 3 goals for and 3 goals against, and the other had 2 goals for and 4 goals against. The are Team 6 (O=3) and either Team 4 (C=4) or Team 8 (T=4), depending on whether the runner-up is Team 4 or Team 8.

    The other teams (T1, T2, T3 and T5) are knocked out in the quarter finals. Team 1 has 1 goal for and 2 goals against (K=2), Team 2 had 0 goals for and 1 goal against (N=1), Team 3 has 0 goals for and 3 goals against (O=3) and Team 5 has 1 goal for and 2 goals against (N=1, K=2).

    The scores in the quarter final matches are 1-0, 2-1, 2-1 and 3-0. The scores in the semi-finals are both 3-0. The score in the final is 4-0.

    These are the two possibilities for the table:

    Enigma 1245 - Solution

  2. Jim Olson 29 March 2015 at 6:51 am

    I think you had a misprint in your comment section in regard to one quarter final match as 3-1.
    It should be 3-0 since all matches prior to the final had less than 4 goals. You obtain the same answer for the enigma with teams 4,6,7 and 8 advancing.

    • Jim Randell 29 March 2015 at 9:32 am

      Yes, that’s right. It should have said the scores in the quarter finals were 1-0, 2-1, 2-1 and 3-0 (which is what the program shows). I’ve fixed it up now, thanks for spotting the typo.

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: