DEV Community

Siddharth
Siddharth

Posted on • Originally published at blog.siddu.tech on

Cracking Wordles using code

If you haven't been living under a rock, you probably have been playingWordle. 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 aWordle Solverwhich can reliably crack wordles in 4 moves or less.

Wordle Solver in action

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

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

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;
}

Enter fullscreen mode Exit fullscreen mode

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

You can try it out yourself by cloning the repo 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!

Top comments (2)

Collapse
 
erangel profile image
Elias Rangel

For a different approach try regular expressions.

Collapse
 
siddharthshyniben profile image
Siddharth

True, I just felt my current method would be more flexible as I add more updates!