Cracking Wordles using code
Tags: javascript, wordle, algorithms
Published: 2022-02-21 09:00:05

---
title: Cracking Wordles using code
published: true
date: 2022-02-21 05:42:25 UTC
tags: javascript, wordle, algorithms
canonical_url: https://blog.siddu.tech/cracking-wordles-using-code
---

If you haven't been living under a rock, you probably have been playing[Wordle](https://www.nytimes.com/games/wordle/index.html). Like a lot of people I kinda started playing it daily, and like a lot of developers I started looking for ways to algorithmically crack it. Eventually I ended up writing a[Wordle Solver](https://github.com/SiddharthShyniben/wordle-solver)which can reliably crack wordles in 4 moves or less.

![Wordle Solver in action](https://github.com/SiddharthShyniben/wordle-solver/raw/main/img/wordle.png)

I just thought I'd walk you through how I created this tool and how it works.

## Finding words which match

The first thing I set out to work on was to find an algorithm which, given a word list and a guess, would filter out the words which can be possible answers to that wordle.

![Possible matches](https://cdn.hashnode.com/res/hashnode/image/upload/v1645417930042/NNpn08BZ_.png)

So I wrote this huge function to compare a guess to see if it matches:

```javascript
function compareGuess(guess, color, answer) {
    const matched = [];
    const len = guess.length;

    for (let i = 0; i < len; i++) {
        if (answer[i] === guess[i]) {
            if (color[i] !== 'g') return false;

            matched.push(i);
        } else {
            if (color[i] === 'g') return false;

            if (color[i] === 'y') {
                const indexes = getAllIndexes(answer, guess[i])
                const filtered = indexes.filter(index => !matched.includes(index))

                if (filtered.length === 0) return false;

                const first = filtered[0];
                matched.push(first);
            }

            if (color[i] === 'k' /* printers */ || color[i] === 'b') {
                const allOccurances = getAllIndexes(answer, guess[i]);
                const filtered = allOccurances.filter(index => !matched.includes(index));

                if (filtered.length > 0 && !(guess[filtered[0]] === answer[filtered[0]])) return false;
            }
        }
    }

    return true;
}

```

I didn't expect it to be so big!

Next I just ran it through a list of all 13 thousand allowed words on wordle and then picked out the best words.

I didn't use the smaller 2k set of possible answers because I felt that the other words could provide more information (I'm not really sure of this but `¯\_(ツ)_/¯`) and also because I didn't know where to find that list 🤣

This works, but there's a problem. It gives us words in alphabetic order and sometimes you can get words like `aahed` which don't really sound like real words and which are unlikely to be the answer.

So, I applied a sorter which sorted words with higher probability first.

![Guessing](https://cdn.hashnode.com/res/hashnode/image/upload/v1645419401187/dDqe-wXjg.png)

You can try it out yourself by cloning the [repo](https://github.com/SiddharthShyniben/wordle-solver) and running `node index.js`

## The future

This solver is not done yet! I've got a few more ideas to implement.

I tried making an algorithm which sorts the words which cut down our list of words the most, but it ended up crashing my computer 🤣. I will try rewriting it in a more efficient language like rust, go or c and give it a try.

Do you have any ideas on how to make this more efficient? Feel free to comment!