I got pointed at a new word game the other day, so the obvious first order of business was to write a solver for it. Waffle is a tile rearrangement game where tile color reveals constraints for the tile placement. It follows the “wordle conventions“, so green means the right letter in the right place, yellow (ochre) means the letter is misplaced belongs somewhere in that answer, and gray means the letter doesn’t belong in the answer at all.

For example, in the puzzle above the yellow “E” in the first row means that row contains an E somewhere, although not in that place. The subsequent gray “E” means there are no additional E’s in that row. The yellow “I” in the first column means that the vertical answer or the horizontal answer contains an I. Note that both answers could contain an I as well. Finally the gray “I” in the bottom row means that answer definitely does not contain an I. For more details I recommend the excellent instructions, which also point out some interesting corner cases.
Computing the fill
The first challenge is finding the “fill” for the puzzle — given the provided grid find a consistent set of answers. I’ll assume the wordle dictionary, aka the list of allowed 5-letter guesses built into the wordle app. This dictionary has 12,946 words, although the actual list of words that could be used for the answer in wordle is much smaller, around a couple thousand. Note, the only reason to use this dictionary is that it is readily available, in particular there’s no reason to believe it’s the dictionary being used by the waffle game.

Approach 1: for each answer in the puzzle try every word in the dictionary and reject choices that don’t work. Assuming the wordle dictionary this gives us
combinations to try (
) which is beyond impractical.
Approach 2: for each answer in the puzzle try every word in the dictionary that matches the known letters in that answer. For example, in the grid above 1 across has 13 possible words (STEEP, STRIP, STOOP, etc.). Unfortunately other entries aren’t so kind: 4 across has a walloping 510 possible matches in the dictionary! Overall, just using the green letters leaves us with
combinations to try. A huge improvement (13 orders of magnitude) but ultimately still terrible.
Approach 3: for each tile in the puzzle figure out the letters it can possibly be, independent of any other tile choices. For example, the N in the first row can’t be an N (because then it would be green, not yellow) and similarly can’t be an L or a P, because those are both gray. Similarly the L to its right can’t be an M because there are no free M tiles anywhere in the grid. With some care we can really whittle down the possibilities we have to consider. In this particular example we’re finally left with these possibilities:
- 1 across, 8 words: STEEP, STIRP, STOEP, STOUP, STRAP, STREP, STRIP, STROP
- 4 across, 4 words: ELMEN, LIMEN, LUMEN, NUMEN
- 5 across, 7 words: TASSE, TEASE, TENSE, TERSE, TOISE, TORSE, TOUSE
- 1 down, 6 words: SALET, SLEET, SLEPT, SPEAT, SPELT, SPLAT
- 2 down, 9 words: ALMAS, OUMAS, RAMAL, REMAN, ROMAL, ROMAN, RUMAL, UNMAN, URMAN
- 3 down, 2 words: PENIE, PENNE
This leaves us with 24,192 combinations (
) to try, or about another 7 orders of magnitude reduction in the search space.
Given a workable approach for finding candidate solutions, all that’s left is to verify that any particular candidate solution is in fact valid. This means that the resulting grid is internally consistent, i.e. a given tile has the same letter assigned to it by both the across fill and the down fill, as well as consistent with the initial puzzle. A valid solution uses exactly the tiles that were present in the original puzzle, and does not contradict any of the information given in the original puzzle. For example, in the puzzle shown above either 1 across or 2 down must contain an N, similarly 3 down must contain an N, 5 across must contain an S, etc.

Computing the solve
Once we know what the final solution looks like the second part of the puzzle is to get there in as few moves as possible. The puzzle starts with 15 moves, and each swap consumes a move. Perfect play will only use 10 moves, achieving a maximum score of 5 stars. If you use all 15 moves you get no stars, but you still succeed, if you use more than 15 moves it’s a loss.
Upper bound analysis
Every swap should put at least 1 tile in the right place1Proof that swapping two tiles without putting one of them in the right place can’t be helpful omitted.. The worst case number of swaps is then bounded by the number of non-green tiles. In the running example that would be 14 swaps, worst case. The actual bound is one less than the number of non-green tiles. Why?
Consider a puzzle with exactly two misplaced tiles: each tile is necessarily in the position where the other misplaced tile should be. Thus a single swap will put both tiles into the correct place. What about a puzzle with one misplaced tile? There is no such puzzle, because a misplaced tile requires a correct place to go to, so a single misplaced tile always implies at least two misplaced tiles. Any number of misplaced tiles N can always be correctly rearranged in at most N-1 moves because we can always make a swap that makes progress (putting at least 1 tile in the right place) and eventually we’ll only have two tiles left and we’ll put them both in the right place with the final swap.
How much better can we do?
We can do better than this upper bound if we find additional pairs of tiles where a single swap puts two tiles into the correct place. The lower bound of the number of swaps required is thus
, as each swap only moves two tiles and clearly each incorrectly placed tile must be moved at least once.
Approach 1: try every possible order of swapping tiles and wait to get lucky. Our upper bound on the number of required swaps is N-1, so the number of permutations to try is something
in our running example. Feasible, but not great.
You are in a maze of twisty little passages, all alike.
One way to look at computing the solve is to construct the graph that shows where each tile needs to ultimately end up. In this graph each tile that’s not in the right place will have exactly one outbound edge, showing where it belongs, and exactly one inbound edge, showing the tile that belongs in its place. An ambiguity immediately arises if there are multiple misplaced tiles with the same letter — does it matter which instance of each letter goes where, and if so, how do we decide?

In the above the duplicate misplaced tiles have been subscripted to distinguish them. We can ignore the green tiles because they can’t be moved and thus aren’t part of the graph. Note that there are 3 misplaced N tiles and 3 misplaced E tiles in the puzzle. The rearrangement of the E tiles in one solution has been highlighted. Any permutation of the E tiles would be valid (
) as well as any permutation of the N tiles, so in this case there are
solutions to the puzzle, differing only in how we choose to assign the E and N tiles to destinations. While all of these solutions are equally valid, they may differ in how many swaps are required to reach them. In order to maximize our score in the game we want to minimize swaps, so how do we identify the solution(s) with the fewest swaps?

The complete graph for one solution, shown above, contains 4 cycles. This is a consequence of how we chose to assign the repeated tiles to particular locations in the solution. The tiles in a given cycle of C tiles can all be moved into place in exactly C-1 swaps. This is obvious for a cycle of 2 tiles, which requires a single swap, and follows straightforwardly for larger cycles. For example, in a cycle of 3 tiles the first swap will put one tile in the correct place necessarily, leaving a cycle of two tiles, which we already now can be solved in a single swap, for
total swaps. The argument extends to larger cycles similarly. So in this example there is a cycle of 7 tiles, a cycle of 3 tiles and 2 cycles of 2 tiles, and will require a total of
swaps to solve. Not coincidentally the waffle game budgets 15 swaps to solve the puzzle, and a perfect 5-star score is achieved by using only 10 swaps.
Approach 2: enumerate all the possible assignments of misplaced tiles to destination locations and of all these candidate solutions, choose the solution with the most cycles, and thus the fewest swaps.
In general approach 2 works well because the number of permutations to consider doesn’t get very large — tens or hundreds perhaps. However, what if we make the problem harder?
Bigger
Every week there is a “deluxe waffle” puzzle to solve, with a
waffle and a 25 swap budget. How do these algorithms scale up?

Here’s an example 7×7 puzzle. The solver does just find identifying a set of words that solve the puzzle. Overall it tested 672 possible combinations of words, in half the cases only having a single candidate word for a given answer. Computing an optimal solve (fewest swaps) is a different story, the problem is there’s room for so many more duplicate tiles in a bigger puzzle. In particular there are 6 E’s, 4 U’s, and smaller multiples of other letters, for a total of 829,440 different ways of mapping the misplaced tiles to a solve. The best solves require 20 swaps, and not surprisingly the game allocates 25 swaps, so a 5-star score is possible, but only just.
Perfect is the enemy of the good
Attempting to optimize the computation of the solve was an interesting rathole, but still a rathole. I think the optimization amounts to:
- constructing the graph of all the misplaced tiles with an edge from a particular misplaced letter to each letter it should replace, and then
- finding a maximal set of edge disjoint cycles in the graph.
For example, that graph for the above 7×7 puzzle is shown below.
While it is easy to find a maximal set of edge-disjoint cycles by inspection, it is unclear to me that it is “easy” to do in the sense of computing it in polynomial time. I think the best you can do is the product of the factorial of the outdegree of every vertex, which is a roundabout way of saying “exhaustive search of a space that can grow exponentially in the number of tiles.”
- 1Proof that swapping two tiles without putting one of them in the right place can’t be helpful omitted. ↩︎

