Books · Solving Puzzles through Mathematical Programming
Chapter 59
The Colour Fairies
Five fairies drift through a nursery on five nights, and each is smitten with a single colour. Above each sleeping child hangs a little cluster of coloured stars, and when a visiting fairy finds her colour among a child’s stars she leaves a pearl behind. We are not told which colour any fairy loves; we are told only the stars each child displays, the count of pearls each child woke to on each night, and which fairies flew on which night. From that we are to name every fairy’s colour. The puzzle is Dennis Shasha’s, and the integer-programming treatment is Chlond’s sequel to his shifty-witnesses lie detector; once more the trick is to let the unknown loves and the known counts share one program and force them to agree.
The puzzle
There are three children, Tyler, Jordan and David, and nine colours: silver, sage, gold, rose, turquoise, ivory, violet, emerald and earth. The stars above each child are fixed for the week, Five fairies, Cloe, Ariana, Oliviana, Anya and Caroline, visit on the nights and the pearls found the next morning, child by child over nights to , were Two facts close the puzzle: at least one fairy loves turquoise, and exactly one loves earth. Which colour does each fairy love?
The model
Each fairy loves one colour, so the natural unknown is a grid. Write when fairy loves colour , and ask each row to choose exactly one colour, The pearls do the rest. Let when child displays a star of colour , and when fairy visits on night . On night child gains a pearl from every visiting fairy whose colour sits among the child’s stars, so the morning’s tally must satisfy The product looks quadratic but is not: and are known numbers, and only is a variable, so every one of these fifteen constraints is linear. Two cardinality clues finish the statement, There is nothing to maximise; any grid obeying every line is the answer.
The whole clue set as one matrix
It is worth seeing the pearl constraints whole. Gather the loves into a matrix (fairies by colours), the stars into a matrix (children by colours), and the visits into a matrix (fairies by nights). Then is a matrix whose entry is exactly when fairy pleases child , since each fairy’s row of holds a single one; multiplying by sums those over the night’s visitors. The fifteen pearl equations are then the single statement with the pearl table. A whole genre of consistency clue collapses to one matrix product, which is how the puzzle is most naturally laid out in a spreadsheet, and a reminder that sigma-notation and matrix-notation are two readings of the same constraint.
The solver
from ortools.sat.python import cp_model
COLORS = ["silver", "sage", "gold", "rose", "turquoise",
"ivory", "violet", "emerald", "earth"]
FAIRIES = ["Cloe", "Ariana", "Oliviana", "Anya", "Caroline"]
star = [[0, 0, 0, 1, 1, 0, 1, 0, 0], # Tyler
[0, 1, 0, 0, 0, 1, 1, 0, 0], # Jordan
[0, 1, 0, 0, 0, 0, 1, 1, 0]] # David
pearl = [[1, 0, 0, 1, 1], [1, 2, 1, 1, 2], [2, 2, 2, 0, 1]]
visit = [[1, 0, 0, 1, 1], [0, 0, 1, 1, 0], [0, 1, 0, 1, 1],
[1, 1, 1, 0, 1], [1, 1, 1, 0, 0]]
def solve_fairies():
F, H, C, N = 5, 3, 9, 5
m = cp_model.CpModel()
x = {(i, k): m.NewBoolVar("")
for i in range(F) for k in range(C)}
# each fairy loves exactly one colour
for i in range(F):
m.Add(sum(x[i, k] for k in range(C)) == 1)
# pearls each night match the visits and the stars
for j in range(H):
for t in range(N):
got = sum(visit[i][t] * star[j][k] * x[i, k]
for i in range(F) for k in range(C))
m.Add(got == pearl[j][t])
# at least one turquoise, exactly one earth
m.Add(sum(x[i, 4] for i in range(F)) >= 1)
m.Add(sum(x[i, 8] for i in range(F)) == 1)
s = cp_model.CpSolver()
s.Solve(m)
out = {}
for i in range(F):
k = next(c for c in range(C) if s.Value(x[i, c]))
out[FAIRIES[i]] = COLORS[k]
return out
The program reads off the unique loves of Figure 59.1: Cloe loves turquoise, Ariana earth, Oliviana ivory, Anya sage and Caroline emerald. No other grid satisfies every line, so the answer is forced.
Reading the deduction
The pearls never see a fairy’s colour directly, only which children it pleases. A colour is therefore known to the tallies merely by its please-signature, the triple recording whether it sits among Tyler’s, Jordan’s and David’s stars. Sorting the nine colours by that signature is telling: Four of the signatures belong to a single colour, but two are shared: rose and turquoise please exactly Tyler, and silver, gold and earth please no one. The pearl tallies pin each fairy’s signature, and so they pin outright the three fairies whose signature is unique to its colour: Oliviana must be the lone ivory, Anya the lone sage, Caroline the lone emerald. They cannot, however, separate fairies inside a shared class. Cloe’s signature is , leaving her rose or turquoise; Ariana’s is , leaving her silver, gold or earth. The counts alone thus admit readings of the nursery, all pleasing the children identically.
This is where the two soft clues earn their place, and they are placed with care. “At least one turquoise” can be honoured by no fairy but Cloe, the only one with turquoise in reach, so it forces Cloe to turquoise and settles her class. “Exactly one earth” can be met by no fairy but Ariana, so it forces Ariana to earth and settles hers. Each clue names one colour inside one shared class and breaks that tie alone; drop either and the count of consistent nurseries climbs back from one to three or to two. The hard data fixes the structure, the loves the children can feel, and the soft data supplies the two labels the structure leaves free, a division this book has met before and will meet again.
Sources. The puzzle and its integer program follow Martin J. Chlond, Puzzle—O.R. with the Fairies, INFORMS Transactions on Education volume , number (), pages –; the matrix reading is his. It is the sequel to his shifty-witnesses lie detector of the previous year. The puzzle is Dennis E. Shasha’s, from Puzzling Adventures (W. W. Norton, ) and first set in his Scientific American column of April . The loves recovered here, the uniqueness of that reading, the six pearl-only readings and their structure, the partition of the colours by please-signature, and the necessity of each cardinality clue are checked by exhaustion over all colourings.