Analytics Vidhya
Published in

Analytics Vidhya

Taking the fun out of WORDLE 😈

AlphaWordle ⚡️

Using Information Theory to win WORDLE — every day! 😎

In this blog, I will discuss how Decision Trees can be used to build a bot that wins WORDLE for you. Want to try it out? Here’s an interactive demo

Source: Unsplash | Sergei A

Wait. What? 😳

Every game developer on this planet questioned their life choices when news broke out that the New York Times acquired WORDLE for over USD 1 mn earlier this month. Everyone else, however, seems to be trying to figure out the best starting word to play the game. 😏

I find most of my friends split up between those who wake up and solve the day’s WORDLE first thing in the morning and those who solve the day’s WORDLE three minutes past midnight. If you still haven’t heard of this game, I highly recommend crawling out of whatever rock you have been living under! 😬

The instructions are pretty simple and the webpage does a great job of explaining the game. I am not going to link it here (just google it 😜)

Fig. 1: WORDLE Instructions

Approach — The WORDLE winning strategy 📋

Here’s the TL;DR version:

Create a candidate_list of all possible five-letter english words
Create an empty rule_list of rules that stores the feedback you get
Repeat until you guess the WORDLE:
Pick a word from the candidate_list with max information gain (TBD)
Obtain the feedback from the game and update the rule_list
Update the candidate_list to remove words that defy the rule_list

candidate_list

The folks over at Stanford have put together this nice list of five-letter words. We use it as a starting point for our candidate list. This list has 5757 valid five-letter words and I haven’t come across a WORDLE that isn’t included in this list (yet). Do let me know if there is a better database available elsewhere!

rules_list

We will break the rule_list into two: tautology and contradictions. The first list will store all the rules that the WORDLE must satisfy (Think of the GREEN boxes) and the second list is used to store the rules that are definitely not satisfied by the WORDLE (Think of the BLACK boxes, I call them RED because as you’ll see later BLACK boxes in WORDLE are not exactly contradictions).

A rule is a two-tuple of the form (alphabet, position) where an alphabet is a character in A-Z and position is a number from 0 through 4 (both inclusive) that indicates the position of the character in the word.

Example: Let’s say the rule is A2. If it is present in the tautologylist, then any word that does not have an “A” in its third (remember: we start counting from zero) position is kicked out of the candidate_list. However, if the same rule is present in the contradictionslist, then any word that does have an “A” in its third position is kicked out of the candidate_list.

We observe that a tautology rule is much more powerful than a contradiction rule in terms of reducing the candidate search space. Obviously, it is harder to find a tautology rule.

making a guess

Rules (as described above) are not infinite. For an English WORDLE of length 5, there are only 130 distinct possible rules (5 times 26). Each rule divides the candidate_list into two groups: the list of candidates that satisfy that rule and the list of candidates that don’t.

So our strategy for making a guess is very simple (greedy):

unverified_rules is a list of rules who have neither been proven true nor falseMake a local_copy of the unverified_rulesRepeat 5 times:
From local_copy, pick the rule with max Information Gain
From local_copy, remove rules that can't be tested with picked rule
# We can't test A2 and B2 in the same guess!
# Because the third position can be filled with A XOR B
Build a word from the five picked rules and that's our guess! (TBD)

It is guaranteed that we will always be able to build a word out of the five picked rules because we picked each rule for its max IG, sequentially.

For that reason, a rule which is defied by all candidates (or satisfied by all candidates) will never be picked and we can be certain that there will at least be one word in the candidate list that satisfies any combination of the five rules we picked to build a guess.

feedback

The user is expected to give feedback in an RGY string.

R: is used to indicate that the corresponding character occurs NOWHERE in the WORDLE.
G: is used to indicate that the corresponding character occurs at the correct position in the WORDLE.
Y: is used to indicate that the corresponding character exists in the WORDLE but not at the correct position.

Let us walk through an example of processing the feedback to update the rules_list

If the guess was “PLACE” and the feedback entered was “RGYRG”, we would:

  • Add P0, P1, P2, P3 and P4 into the contradictions list indicating that “P” occurs nowhere in the WORDLE.
  • Add L1 into the tautology list indicating that all candidates must have “A” in the second position.
  • Add A2 in the contradictions list indicating that “A” must not occur at the third position in any of the candidates.
  • Add C0, C1, C2, C3 and C4 into the contradictions list indicating that “C” occurs nowhere in the WORDLE.
  • Add E4 into the tautology list indicating that all candidates must have “E” in the second position.

… and this goes on until we get a GGGGG indicating the WORDLE has been guessed! 🌑 🌒 🌓 🌔 🌝

Code 🚀

TL;DR: Goto this demo notebook.

🚨 Providing RGY feedback works a little differently in alpha-wordle 🚨

Give R feedback only when you want to convey that the corresponding alphabet does not occur anywhere in the WORDLE. Do not use R on the second occurrence of an alphabet if the alphabet occurs only once in the WORDLE. Use a Y instead to indicate wrong position.

Example: If the WORDLE is “STEAL” and the engine guesses “AAAAA” (Yes, that’s a proper English word. It means I am annoyed because you’re interrupting my example 💁) the feedback you should provide is: YYYGY and not RRRGR

usage 👶🏻

There are no dependencies and you should be able to install it on any platform with Python≥3.6.

To install the PyPI package use.

> pip3 install alpha-wordle

If you’d like to install it for lower versions of Python3 (not supported), use:

> git clone https://github.com/ivbhatt/alpha-wordle
# Go into setup.py and change the Python version requirements.
> python3 setup.py sdist bdist_wheel # build the package
> pip3 install -e . #install the trial package to curr_dir

To start the engine:

import alphawordle as a
a.start()
---
You should be prompted for an RGY feedback. Here is the prompt after a sample run.
---
Guess is: BALES
Enter RGY feedback: RGRRR
Guess is: CANDY
Enter RGY feedback: YGRRR
Guess is: MAFIA
Enter RGY feedback: RGRGY
Guess is: TACIT
Enter RGY feedback: GGGGG
Done!

contributing 💙

  • GitHub 👷: https://github.com/ivbhatt/alpha-wordle
  • MIT-License: Use the code any way you like it 💃
  • 📢 Code-refactoring, Documentation, Bug-fixes, Feature-additions — everything is welcome :)
  • You know the drill: fork, commit & raise a PR (or just ⭐️ ️and move on!)

Bye!

Until next time.

Source: Unsplash | Thomas Lipke

 by the author.

--

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Recommended from Medium

CS371p Spring 2022: Max Thomas

Project Quantum Game Dev Diary — Mar. 3rd, 2022— Jason Tennent

Work-in-progress sniper weapon model

CS371P Blog: Week of 19 Apr — 25 Apr

Part 2: A comparison of CRUD and CQRS patterns using shopping cart application

Pricing Options using BSM.

Eye on the Prys

Hashtag #hashmaps

30 Days Coding Challenge — Day 17

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ishan Bhatt

Ishan Bhatt

graduate student (MS CS) & research assistant (PICTure Lab) @ NCSU, Raleigh; Ex systems engineer @ TCS — R&I, India; B. Tech. (Computer Engg.) from BVM, India.

More from Medium

Grasp and Lift EEG classification using CWT of Electroencephalography (EEG) signals in Python, A…

University Of Hertfordshire School Of Computer Science

Housing Bubbles in History — A Detection Using Python

LIMITS of SQL — Indeed there is a Better, oh… Best Alternative!