Skip to content

Repository files navigation

Wordle-Solver

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.

Overview

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.

Key Concepts

Information Theory Principles

  • 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

Feedback Encoding

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

Project Structure

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

File Descriptions

  • 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

Getting Started

Prerequisites

pip install numpy

Installation

  1. Clone or download this repository
  2. Ensure you have the dictionary and target JSON files
  3. Run the solver:
python main.py

Usage

First Run

On 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

Interactive Mode

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
  1. The solver suggests the best guess (e.g., "salet")
  2. Enter your guess (can be the suggested word or any valid word)
  3. Enter the feedback pattern using 'r', 'y', 'g' (5 characters)
    • Example: ggryy means: green, green, gray, yellow, yellow

Input Format

salet          # Your guess
rryrg          # Feedback: gray, gray, yellow, gray, green

Example Session

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

Algorithm

1. Initialization

- Load dictionary (all valid guesses)
- Load targets (possible answers)
- Precompute or load pattern matrix

2. Main Loop

For each turn:

  1. Calculate Prior Entropy: H(W) = log₂(candidate_count)

  2. Find Best Guess:

    • For each possible guess word
    • Calculate expected feedback entropy H(Y)
    • Select guess with maximum H(Y)
  3. Display Information:

    • Prior entropy
    • Expected feedback entropy
    • Posterior entropy
    • Information gain
    • Best guess recommendation
  4. Update Candidates:

    • Read user's actual guess and feedback
    • Filter candidates to match the feedback pattern
    • Repeat until one candidate remains

3. Entropy Calculation

def calculate_entropy(guess_idx):
    patterns = matrix[guess_idx, current_candidates]
    counts = bincount(patterns)
    probs = counts / len(current_candidates)
    return -sum(probs × log₂(probs))

About

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.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages