Square of dominos with equal side sum?

This is an assignment my sister in primary school got. I think they're supposed to solve it via trial and error, but I was wondering if there's a clever way to solve it.

Lay the tiles of a double-six domino set in a a square shape such that adjacent tiles touch with matching values, and that the sum of the values on each side of the square is the same. The target shape:

the target shape

A double-six domino set, from https://en.wikipedia.org/wiki/Dominoes#/media/File:Dominomatrix.svg:

doublesixset


Solution 1:

This is likely not the "clever" solution you had sought, but it does work for a similar problem given to my son as a 2nd-3rd grade problem. His problem was not exactly the same, although in the same flavor.

In this problem, 4 dominos were to be placed to form a square (see Table). Each side of the square had 3 numbers, and the sum of the sides were to be equal. The dominos were (5,3), (3,6), (2,5), and (4,2). The first domino was placed in the upper left corner of the square as shown below. The problem asked how to place the remaining dominos.

col1 col2 col3
5 domin(3) domino(3)
3 domino(2)
domino(1) domino(1) domino(2)

We solved this by writing a computer program in Python, shown below. This was the solution.

col1 col2 col3
5 2 4
3 5
3 6 2

import numpy as np


def flipDominos(dominos, flipOption):
    '''This function flips the dominos'''
    flippedDominos = np.array(dominos, copy=True)
    if flipOption[0]:
        flippedDominos[0, 0] = dominos[0, 1]
        flippedDominos[0, 1] = dominos[0, 0]
    if flipOption[1]:
        flippedDominos[1, 0] = dominos[1, 1]
        flippedDominos[1, 1] = dominos[1, 0]
    if flipOption[2]:
        flippedDominos[2, 0] = dominos[2, 1]
        flippedDominos[2, 1] = dominos[2, 0]
    return flippedDominos


# these are the dominos we can place
dominos = np.array([[2, 4], [3, 6], [5, 2]], np.int32)

# this is the order we can place them
orderOptions = np.array([[1, 2, 3],
                         [1, 3, 2],
                         [2, 1, 3],
                         [2, 3, 1],
                         [3, 1, 2],
                         [3, 2, 1]], np.int32)
orderOptions += -1  # 0-index means we need to subtract 1

# ways we can flip the dominos
flipOptions = np.array([[True, True, True],
                        [True, True, False],
                        [True, False, True],
                        [True, False, False],
                        [False, False, False],
                        [False, False, True],
                        [False, True, False],
                        [False, True, True]], bool)

# an array for all the dominos
# columns are for 8 numbers from dominos, 4 sides to add, and a bool to indicate a possible solution
dominoCombination = np.zeros((len(orderOptions) * len(flipOptions), 8 + 4 + 1), np.int32)

# first domino
dominoCombination[:, 0] = 5
dominoCombination[:, 1] = 3

# place other dominos
rowId = 0
for orderOption in orderOptions:
    for flipOption in flipOptions:
        flippedDominos = flipDominos(dominos, flipOption)
        dominoCombination[rowId, 2] = flippedDominos[orderOption[0], 0]
        dominoCombination[rowId, 3] = flippedDominos[orderOption[0], 1]
        dominoCombination[rowId, 4] = flippedDominos[orderOption[1], 0]
        dominoCombination[rowId, 5] = flippedDominos[orderOption[1], 1]
        dominoCombination[rowId, 6] = flippedDominos[orderOption[2], 0]
        dominoCombination[rowId, 7] = flippedDominos[orderOption[2], 1]
        rowId += 1

# sum sides
dominoCombination[:, 8] = dominoCombination[:, 0] + dominoCombination[:, 1] + dominoCombination[:, 2]
dominoCombination[:, 9] = dominoCombination[:, 2] + dominoCombination[:, 3] + dominoCombination[:, 4]
dominoCombination[:, 10] = dominoCombination[:, 4] + dominoCombination[:, 5] + dominoCombination[:, 6]
dominoCombination[:, 11] = dominoCombination[:, 6] + dominoCombination[:, 7] + dominoCombination[:, 0]

# are sum sides the same?
i = 0
for row in dominoCombination:
    if ( dominoCombination[i, 8] == dominoCombination[i, 9]
        and dominoCombination[i, 8] == dominoCombination[i, 11]
        and dominoCombination[i, 8] == dominoCombination[i, 10]):
        dominoCombination[i, 12] = 1
    i += 1
print dominoCombination