Wordle solver

For more than 60 days, about the first thing I do is to (try to) solve the Wordle puzzle (Wordle - The New York Times). For the very first days I did that manually, but then wrote a solver (i.e. a cheater) instead. The hardest part of this was to find a good enough sorting principle of the possible words (the candidates) and a good first guess word. After quite a lot of experiments I found something that is fairly good; right now this solver have an average of about 3.6 (of possible 6), and no misses.

Today I wrote a solver in SWI-Prolog: http://hakank.org/swi_prolog/wordle.pl .

The interface to the solver is as follows:

wordle(Words,CorrectPos,CorrectChar,NotInWord)
- Words: the wordlist/candidates
- CorrectPos: correct character in correct position,
  Example: n is in position 4 and t is in position 5: "...nt": 
- CorrectChar: correct character but in wrong position.
  Example: a is not in position 1, l is not in position 2: ["a","l","","",""]
- NotInWord: characters not in word.
  Example: s, a, and l are not in the word: "sal"

Here is an example:

wordle(".r.n.",["","","","",""],"slatcoe").

Given a certain wordlist, it yield the following candidates, sorted according to the scoring principle:

candidates=[briny,brink,grind,bring,drink,drunk,wring,wrung]
len=8
suggestion=briny

Perhaps this is useful for some…

8 Likes

Nice! I also made a Prolog wordle “assistant” – source here, online here

2 Likes

That’s great!

The last days I’ve been playing with DCGs, and thought that it would be fun to do a Wordle solver using DCGs. Here it is: http://hakank.org/swi_prolog/wordle_dcg.pl .

It uses DCGs for the main checking operations (I’ve kept some plain non-DCG predicates ), and it also includes a (quite large) DCG for generating the 2315 Wordle target words (see below for a little more on this).

Here’s the run from yesterday (solved in 4/6):

?- [wordle_dcg].
% ...
?- empty.
wordle(".....",["","","","",""],"").

?- wordle(".....",["","o","","e","e"],"slantcridvgu").
wordle(words,.....,[,o,,e,e],slantcridvgu)
candidates=[epoxy]
len=1
suggestion=epoxy

Below is the start of the big DCG for generating the Wordle words:

wordle_words --> ("a",("b",("a",("ck";"se";"te");"b",("ey";"ot");"o",("de";"rt";"ut";"ve");"hor";"ide";"led";"use";"yss");"c",("orn";"rid";"tor";"ute");"d",("a",("ge";"pt");"m","i",("n";"t");"o",("r",("e";"n");"be";"pt");"ept";"ult");"f",("o",("ot";"ul");"fix";"ire";"ter");"g",("a",("in";"pe";"te") ...

(It generates all the words and no other words.)

The total length of this DCG is 17 208 characters which is slightly smaller than just listing the strings as alternatives (18 519 chars).

The corresponding regexp is just 9427 characters, and according to my program that generates words from a regexp (not in SWI-Prolog) this is quite slower than using DCGs: Using regexp: 3.53s, using DCG: 0.1s.

1 Like

If you want fast finite state machines: Finite State Morphology
https://web.stanford.edu/~laurik/fsmbook/home.html

These are actually powerful enough to handle natural languages; they have operations to compose, invert, subtract, etc. finite state automata and then optimie them. (Ron Kaplan did quite a bit of work on the optimisation techniques, using Lisp)

1 Like

@peter.ludemann Thanks for the suggestion, Peter. I’ll keep that in mind.

Right now I’m just playing with (perhaps better: obsessed by) DCGs and see how far I can go with them. My interest is not so much natural language processing, rather to be able to parse different type of texts/files (which I’ve done so far with regexps since the 90s, mostly with Perl), and - of course - generate DCGs that generate strings.

A long time ago, I wrote a reversible DCG for C declarations (similar to what https://cdecl.org/ does). I’ve been meaning to re-do it but so far I’ve only scanned in my “tutorial” and I can’t locate my source code, so I’ll probably have to rewrite it from 30-year old memory. If anyone is interested in my tutorial (it uses IBM Prolog syntax, but that probably doesn’t matter), I can point you to it; I don’t want to spread it around much but would prefer to rewrite it.

Also, this book might be of interest: Logic Grammars (Abramson & Dahl)

2 Likes

It would be very interesting to study your tutorial about this!

Thanks for the pointer. It seems to be very interesting. I’ve seen it referenced before, and now I’ll try to get hold of it.