An information theory-based Wordle solver that finds optimal guesses by maximizing entropy and information gain. This implementation uses Shannon's entropy to systematically narrow down possible words with maximum efficiency.
This project implements an intelligent Wordle solver that leverages information theory principles to make optimal guesses. Instead of random guessing, it calculates the expected information gain for each possible guess and selects the one that provides maximum information about the target word.
-
Entropy H(W): Measures the uncertainty in the current set of candidate words
H(W) = log₂(|candidates|)- Higher entropy = more uncertainty
-
Feedback Entropy H(Y): Expected information gained from a guess
H(Y) = -Σ P(pattern) × log₂(P(pattern))- Calculated across all possible feedback patterns
-
Posterior Entropy H(W|Y): Remaining uncertainty after receiving feedback
H(W|Y) = H(W) - H(Y)
-
Information Gain I(W;Y): How much information a guess reveals
I(W;Y) = H(Y)- The solver maximizes this value
Wordle feedback is encoded as a base-3 number:
- 0 (r): Gray - letter not in word
- 1 (y): Yellow - letter in word, wrong position
- 2 (g): Green - letter in correct position
Example: "gryyr" → code = 2×3⁰ + 1×3¹ + 0×3² + 0×3³ + 2×3⁴ = 165
Wordle-Solver/
├── main.py # Main solver implementation
├── dictionary_5_letter.json # All valid 5-letter guess words
├── targets_5_letter.json # Possible target words
├── pattern_matrix.npy # Precomputed feedback matrix (generated)
└── README.md # This file
- main.py: Core solver with entropy calculations and optimal guess selection
- dictionary_5_letter.json: Complete dictionary of valid Wordle guesses (~12,000 words)
- targets_5_letter.json: Subset of common words used as targets (~2,300 words)
- pattern_matrix.npy: Precomputed (G×A) matrix where M[i,j] = feedback pattern for guess i on target j
pip install numpy- Clone or download this repository
- Ensure you have the dictionary and target JSON files
- Run the solver:
python main.pyOn the first run, the solver precomputes the pattern matrix (this may take a few minutes):
Precomputing pattern matrix ...
Pattern matrix precomputed.
Saved pattern_matrix.npy
The solver runs in an interactive loop:
prior entropy H(W) = 11.0882
best-guess expected feedback entropy H(Y) = 5.8882
expected posterior entropy H(W|Y) = 5.2000
information gain I(W;Y) = 5.8882
BEST=salet
- The solver suggests the best guess (e.g., "salet")
- Enter your guess (can be the suggested word or any valid word)
- Enter the feedback pattern using 'r', 'y', 'g' (5 characters)
- Example:
ggryymeans: green, green, gray, yellow, yellow
- Example:
salet # Your guess
rryrg # Feedback: gray, gray, yellow, gray, green
prior entropy H(W) = 11.0882
best-guess expected feedback entropy H(Y) = 5.8882
expected posterior entropy H(W|Y) = 5.2000
information gain I(W;Y) = 5.8882
BEST=salet
salet
rryrg
prior entropy H(W) = 5.3923
best-guess expected feedback entropy H(Y) = 3.1234
expected posterior entropy H(W|Y) = 2.2689
information gain I(W;Y) = 3.1234
BEST=point
H(W)=11.6627
H(Y)=5.9783
H(W|Y)=5.6844
I(W;Y)=5.9783
BEST=soare
soare
rrrrr
H(W)=7.7879
H(Y)=5.6539
H(W|Y)=2.1340
I(W;Y)=5.6539
BEST=clint
clint
yrgrr
H(W)=1.5850
H(Y)=1.5850
H(W|Y)=0.0000
I(W;Y)=1.5850
BEST=aduki
aduki
rryyy
H(W)=0.0000
H(Y)=0.0000
H(W|Y)=0.0000
I(W;Y)=0.0000
BEST=quick
H(W)=11.6627
H(Y)=5.9783
H(W|Y)=5.6844
I(W;Y)=5.9783
BEST=soare
soare
rrryy
H(W)=6.9887
H(Y)=4.2992
H(W|Y)=2.6895
I(W;Y)=4.2992
BEST=lited
lited
rrryr
H(W)=3.1699
H(Y)=3.1699
H(W|Y)=0.0000
I(W;Y)=3.1699
BEST=chump
chump
yyrry
H(W)=0.0000
H(Y)=0.0000
H(W|Y)=0.0000
I(W;Y)=0.0000
BEST=perch
- Load dictionary (all valid guesses)
- Load targets (possible answers)
- Precompute or load pattern matrixFor each turn:
-
Calculate Prior Entropy:
H(W) = log₂(candidate_count) -
Find Best Guess:
- For each possible guess word
- Calculate expected feedback entropy H(Y)
- Select guess with maximum H(Y)
-
Display Information:
- Prior entropy
- Expected feedback entropy
- Posterior entropy
- Information gain
- Best guess recommendation
-
Update Candidates:
- Read user's actual guess and feedback
- Filter candidates to match the feedback pattern
- Repeat until one candidate remains
def calculate_entropy(guess_idx):
patterns = matrix[guess_idx, current_candidates]
counts = bincount(patterns)
probs = counts / len(current_candidates)
return -sum(probs × log₂(probs))