Enigmatic Code

Programming Enigma Puzzles

Enigma 303: Some dominoes

From New Scientist #1451, 11th April 1985 [link]

Most of a full set of dominoes has been arranged in a 7 × 7 block with a hole in the middle. Please mark in the boundaries between the dominoes. There is only one answer.

Enigma 303

[enigma303]

Advertisements

One response to “Enigma 303: Some dominoes

  1. Jim Randell 17 August 2015 at 9:01 am

    You can use the same program used to solve Enigma 179 for this one. All that needs to be changed are the data describing the grid.

    This Python program runs in 41ms.

    from enigma import chunk, printf
    
    # grid dimensions (columns, rows)
    (N, M) = (7, 7)
    
    # number of dominoes in the grid
    D = (N * M - 1) // 2
    
    # marker for unoccupied squares
    X = -99
    
    grid = [
      0, 6, 0, 4, 4, 4, 3,
      0, 2, 3, 2, 2, 2, 5,
      0, 5, 4, 6, 6, 0, 5,
      4, 1, 1, X, 4, 1, 1,
      4, 5, 6, 5, 5, 0, 5,
      6, 3, 3, 3, 2, 6, 5,
      2, 1, 0, 6, 1, 6, 4,
    ]
    
    # update grid <g> placing domino <n> at <i>, <j>
    def update(g, i, j, n):
      g = list(g)
      g[i] = g[j] = n
      return g
    
    # g = grid
    # n = label of next domino (1 to D)
    # D = number of dominoes to place
    # ds = dominoes already placed
    def solve(g, n, D, ds):
      # are we done?
      if n > D:
        # output the pairings
        for r in chunk(g, N):
          print(r)
        print()
      else:
        # find the next unassigned square
        for (i, d) in enumerate(g):
          if d < 0: continue
          (y, x) = divmod(i, N)
          # find placements for the domino
          js = list()
          # horizontally
          if x < N - 1 and not(g[i + 1] < 0): js.append(i + 1)
          # vertically
          if y < M - 1 and not(g[i + N] < 0): js.append(i + N)
          # try possible placements
          for j in js:
            d = tuple(sorted((g[i], g[j])))
            if d not in ds:
              solve(update(g, i, j, -n), n + 1, D, ds.union([d]))
          break
    
    solve(grid, 1, D, set())
    

    Solution: The arrangement of the dominoes is shown below:

    Enigma 303 - 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: