Enigmatic Code

Programming Enigma Puzzles

Enigma 342: A full set of dominoes

From New Scientist #1491, 16th January 1986 [link]

A full set of dominoes has been arranged in a 7×8 block. Please mark in the boundaries between the dominoes. There is only one answer.

Enigma 342

[enigma342]

Advertisements

One response to “Enigma 342: A full set of dominoes

  1. Jim Randell 29 April 2016 at 8:33 am

    You can use the same program used to solve Enigma 179 and Enigma 303 for this one. All that needs to be changed is the data describing the grid. This Python program runs in 48ms.

    from enigma import chunk, printf
    
    # grid dimensions (columns, rows)
    (N, M) = (8, 7)
    
    # number of dominoes in the grid
    D = (N * M) // 2
    
    grid = [
      1, 1, 6, 3, 0, 2, 3, 0,
      1, 2, 4, 3, 2, 2, 1, 6,
      6, 5, 3, 3, 5, 2, 1, 2,
      3, 0, 6, 2, 6, 0, 3, 6,
      4, 4, 5, 4, 0, 0, 5, 4,
      0, 1, 5, 4, 1, 5, 5, 4,
      5, 3, 2, 6, 6, 4, 1, 0,
    ]
    
    # 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 342 - 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: