Fill-in puzzle generator in Python: why one word starts in place
We built 500 fill-in grids from 60 Christmas themes. Without a word printed in place, 22 to 29 % could be filled in two ways, with HOPE and LOVE trading places. One starting word fixes almost all of them.
A fill-in puzzle, also called a fill-it or criss-cross puzzle, is a crossword without clues: an empty grid and the list of its words, grouped by length. The solver fits every word in so that crossing words share their letters. A generator has to give the solver a grid that fills in only one way.
The grids here come from best_grid() in our crossword generator post. On 31 October 2026 we counted the ways to fill them, on the 60 Christmas and winter themes of our puzzle books (1,425 words).
The short version#
- No starting word: easy grids (words of 3 to 7 letters, 14 drawn) filled in one way in 78 % of 300; medium grids (3 to 10 letters, 20 drawn) in 71 % of 200. The rest had two ways. Typically two words of the same length traded places where they cross matching letters: HOPE and LOVE, GOOSE and ROAST, DONNER and HOOVES.
- One word printed in place: for 97 % of easy and 94 % of medium grids, some word made the grid fill in one way. On average 81 % of the words in an easy grid would work as the start, and 73 % in a medium one.
- The generator: try each word as the starting one, and draw a new grid when none works. That took 1.03 grids a puzzle on average, about 10 ms.
The code#
import random
from crossword import best_grid, show # from our crossword generator post
def fillings(placed, given=None, stop=2):
"""The ways the listed words fit the empty grid (up to `stop` of them): every word once, crossing squares agree.
`placed` is [(word, row, col, direction)]; `given` is the index of the word printed in place to start."""
words = [w for w, r, c, d in placed]
cells = [[(r + d[0] * i, c + d[1] * i) for i in range(len(w))] for w, r, c, d in placed]
grid, filled, used, found = {}, {}, set(), []
def put(k, w):
new = [cell for cell in cells[k] if cell not in grid]
for cell, ch in zip(cells[k], w):
grid[cell] = ch
filled[k] = w
used.add(w)
return new
def take(k, new):
used.discard(filled.pop(k))
for cell in new:
del grid[cell]
def go():
if len(found) >= stop:
return
if len(filled) == len(words):
found.append(dict(filled))
return
options = {k: [w for w in words if w not in used and len(w) == len(cells[k])
and all(grid.get(cell, ch) == ch for cell, ch in zip(cells[k], w))]
for k in range(len(words)) if k not in filled}
k = min(options, key=lambda x: len(options[x])) # the slot with the fewest words that fit
for w in options[k]:
new = put(k, w)
go()
take(k, new)
if given is not None:
put(given, words[given])
go()
return found
def make(theme_words, count=14, shortest=3, longest=7, rng=random, tries=100):
"""A grid and a starting word with which it fills in exactly one way: draw words from the theme, build the
grid, try each word as the starting one; if none works, draw again."""
pool = [w for w in theme_words if shortest <= len(w) <= longest]
for k in range(1, tries + 1):
grid, placed = best_grid(rng.sample(pool, min(count, len(pool))), rng=rng)
for given in rng.sample(range(len(placed)), len(placed)):
if len(fillings(placed, given)) == 1:
return grid, placed, given, k
raise RuntimeError("no grid that fills in one way")grid, placed, given, draws = make(themes["Trimming the Tree"], rng=random.Random(2))
print(show(grid))
print("words:", len(placed), "| starting word:", placed[given][0], "| ways:", len(fillings(placed, given)),
"| without it:", len(fillings(placed)))With that seed it built this grid of 14 words and picked SPARKLE to start:
. . . . . . . . . . . B
. . B . . S T A R . . E
. . A . . P . . . . . A
. . U . . R . . . P . D
. . B . . U . H O O K S
A . L . . C . . . P . .
N E E D L E S . . C . .
G . . . . . P . . O . .
E . . . . . A . . R . .
L . . . G A R L A N D .
. . . . . . K . . . . .
. . . L . . L . . . . .
. I C I C L E S . . . .
. . . G . . . T . . . .
. . . H . . . R . . . .
. . . T . . . I . . . .
T I N S E L . N . . . .
. . . . . . . G . . . .This one would fill in one way even without SPARKLE. About one easy grid in five would not.
Why one word is enough#
HOPE and LOVE can trade places only where the grid asks for the same letters from both: the O and the E. Print either of them in place, or a word that crosses one of them at the H, P, L or V, and the swap is gone. fillings() checks this directly: it places the starting word first and then counts every way to place the rest, up to two.
Where two ways were left, they differed in two words in the median case, and in four at most. So the second way is usually one swap, which a solver finds by accident and the answer key calls wrong.
Counting is cheap at this size, because fillings() always fills the slot with the fewest words that still fit. All the checks for one grid, with no starting word and then each word in turn, took 8 ms (median) for an easy grid and 23 ms for a medium one.
A whole book, not one grid#
Our fill in book maker checks every puzzle the same way: a second program, written apart from the one that makes the puzzles, tries every way to fit the listed words into the empty grid, starting from the word already in place, and finds exactly one. Every run of two or more boxes is a word from the list, each word is in the grid once, the grid is one connected piece, and no word touches another side by side.
Easy books use words of 3 to 7 letters, up to 14 a puzzle; medium 3 to 10 letters, up to 20; hard 3 to 12 letters, up to 26. You can use our 60 Christmas and winter themes and 103 everyday ones, and the book is checked against KDP's paperback specs before you can download it. One fill in book costs 500 credits, from $3.30 in the big pack.
written
5 min readKDP margins and gutter: check your PDF before you upload it
KDP's inside margin grows with the page count, and a book of 151 pages needs more than one of 150. The table from KDP's help, and forty lines of Python that find the text too close to an edge.
5 min readFutoshiki generator in Python: signs alone often are not enough
A 5 x 5 futoshiki has 40 places for a sign. Even with all 40 filled in, 14 of our 40 squares still had more than one answer. A generator that adds starting digits first and then removes signs gets there in 0.3 s.
19 min readKDP coloring book size and bleed: check your interior PDF in Python
The trim sizes KDP lists, when a coloring book needs bleed, why the page count doubles and what 300 ppi means. Then a Python check that reads your interior PDF and says where it breaks KDP's published rules.