Enigmatic Code

Programming Enigma Puzzles

Enigma 1360: An Enigma gem

From New Scientist #2519, 1st October 2005

Joe reckons that the latest popular puzzle is based on a much older puzzle in which the same set of letters were placed in each row, each column and each diagonal of a grid. This is part of Joe’s attempt at a 6-by-6-grid, which he has dedicated to his parents.

Enigma 1360

What 6 letters are missing from the second row (left to right)?

[enigma1360]

Advertisements

One response to “Enigma 1360: An Enigma gem

  1. Jim Randell 27 December 2013 at 8:24 am

    You can solve this puzzle using a simple recursive program (see Enigma 1780), or with a more strategic based solver (see Enigma 1657), but here’s a program that uses PyMathProg to solve the puzzle using ILP. It runs in 69ms.

    import pymprog
    from enigma import irange, printf
    
    # create the model
    m = pymprog.model('enigma1360')
    
    # x[i, j, k] is true if letter k is at position (i, j)
    I = list(irange(0, 5))
    K = list('ENIGMA')
    x = m.var(pymprog.iprod(I, I, K), 'x', bool)
    
    # each cell is assigned a letter
    m.st(sum(x[i, j, k] for k in K) == 1 for i in I for j in I)
    
    # each letter appears once in each column
    m.st(sum(x[i, j, k] for j in I) == 1 for i in I for k in K)
    
    # each letter appears once in each row
    m.st(sum(x[i, j, k] for i in I) == 1 for j in I for k in K)
    
    # each letter appears once in each diagonal
    m.st(sum(x[i, i, k] for i in I) == 1 for k in K)
    m.st(sum(x[i, 5 - i, k] for i in I) == 1 for k in K)
    
    # given values
    m.st(x[0, 3, 'A'] == 1)
    m.st(x[0, 4, 'N'] == 1)
    
    m.st(x[0, 5, 'E'] == 1)
    m.st(x[1, 5, 'N'] == 1)
    m.st(x[2, 5, 'I'] == 1)
    m.st(x[3, 5, 'G'] == 1)
    m.st(x[4, 5, 'M'] == 1)
    m.st(x[5, 5, 'A'] == 1)
    
    m.st(x[5, 2, 'G'] == 1)
    m.st(x[5, 3, 'E'] == 1)
    m.st(x[5, 4, 'M'] == 1)
    
    # solve the puzzle
    m.solve()
    
    # determine the solution
    rows = list(list(list(list(k for k in K if x[i, j, k].primal)) [0] for i in I) for j in I)
    for x in rows:
      printf("[{x}]", x=' '.join(x))
    
    printf("second row = {x}", x=' '.join(rows[1]))
    

    Solution: The letters in the second row are G E M N A I.

    There are two possible arrangements of the grid, but the PyMathProg solution stops when it finds a solution.

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: