DEV Community

dolai
dolai

Posted on

A daily word ladder is a graph problem wearing a cozy sweater

A word ladder looks like a party trick: start on one four-letter word, change one letter per step, and land on a target. Underneath it is a shortest-path problem on a graph where the nodes are words and the edges are single-letter substitutions. Building one for a real audience made that abstraction concrete, so here is what the graph actually implies.

The edges are the whole product

Take a dictionary of four-letter words. For every word, generate the neighbors that differ in exactly one position. A naive implementation compares every pair, which is quadratic and pointless: with 2,586 valid words you can instead bucket by wildcard patterns (_eet, f_et, fe_t, fee_) and only compare words inside a bucket. Bucket size stays small, and the neighbor list comes out in linear time.

That neighbor list is the graph. Par is then a breadth-first search from the starting word to the target, run once at build time and stored with the puzzle. If the BFS never reaches the target, the starting word is invalid and should not be scheduled.

Scheduling is where the honesty lives

A daily puzzle site has one commitment that matters: a past puzzle must keep its original meaning. That means the dictionary, the schedule and the par value all have to be frozen together. If you ship v1 with 2,586 words and later prune or add a word, an old ladder can silently become unsolvable or easier than the par it shipped with. Pinning the word list per version, and storing the computed par next to the puzzle rather than recalculating it on request, avoids that whole class of bug.

I applied this to feetle game, a free daily four-letter ladder where the target is always FEET. The same dictionary feeds the solver, the past-answer archive and the par calculation, so a route found today still validates tomorrow. Progress lives in local storage instead of an account table, which keeps the backend down to static files and a frozen schedule.

Small lessons worth keeping

  • Compute heavy invariants at build time. Par is cheap to store and expensive to recompute under load.
  • Freeze inputs, not just outputs. A versioned word list is what makes an archive trustworthy.
  • Let the UI stay boring. A keyboard-first board, a countdown to 08:00 UTC, and one shared ladder per day is enough structure for a daily habit.

The interesting part of a word puzzle turned out not to be the words at all; it was deciding what to freeze so the puzzle stays fair across months.

Top comments (0)