I was lurking in a group chat last week where people were arguing about Wordle. Apparently someone got it in 2 and everyone lost their mind. I hadn't played Wordle in my life (genuinely zero interest) but the math angle of the argument caught me.
Wordle gives you 6 tries to guess a 5-letter word. After each guess, you get colored feedback:
- Green — right letter, right position
- Yellow — right letter, wrong position
- Gray — letter not in the word
To determine which word should you guess next to eliminate the most possibilities,
I used entropy from information theory.
For a given guess, you look at all possible feedback patterns it could produce against the remaining candidate words.
The more evenly it splits them, the more information you gain regardless of the outcome. You pick the guess that maximizes that split.
Formally, for a guess g against a pool of possible answers P:
H(g) = -Σ p(pattern) * log2(p(pattern))
Where each pattern is one of the 243 possible G/Y/X combinations (3⁵),
and p(pattern) is the fraction of words in P that produce that pattern. High entropy = good guess.
P.S.S.: I used this wordlist with 14,855 wordle answers.
from collections import Counter
import math
def result(guess, answer):
res, pool = [0]*5, list(answer)
for i in range(5):
if guess[i] == answer[i]:
res[i] = 2
pool[i] = None
for i in range(5):
if res[i] != 2 and guess[i] in pool:
res[i] = 1
pool[pool.index(guess[i])] = None
r, m = 0, 1
for v in res:
r += v * m
m *= 3
return r # base-3 encoded: G=2, Y=1, X=0
def entropy(guess, possible):
counts = Counter(result(guess, w) for w in possible)
total = len(possible)
return -sum((c/total) * math.log2(c/total) for c in counts.values())
def best_guess(possible, full_wordlist):
return max(possible, key=lambda w: entropy(w, possible))That's basically it.
Each round you filter the pool based on result, then pick the next guess with the highest entropy score against what's left.