Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Siddharth
Siddharth

Posted on • Originally published atblog.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:

functioncompareGuess(guess,color,answer){constmatched=[];constlen=guess.length;for(leti=0;i<len;i++){if(answer[i]===guess[i]){if(color[i]!=='g')returnfalse;matched.push(i);}else{if(color[i]==='g')returnfalse;if(color[i]==='y'){constindexes=getAllIndexes(answer,guess[i])constfiltered=indexes.filter(index=>!matched.includes(index))if(filtered.length===0)returnfalse;constfirst=filtered[0];matched.push(first);}if(color[i]==='k'/* printers */||color[i]==='b'){constallOccurances=getAllIndexes(answer,guess[i]);constfiltered=allOccurances.filter(index=>!matched.includes(index));if(filtered.length>0&&!(guess[filtered[0]]===answer[filtered[0]]))returnfalse;}}}returntrue;}
Enter fullscreen modeExit 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 likeaahed 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 therepo and runningnode 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
erangel profile image
Elias Rangel
  • Joined

For a different approach try regular expressions.

CollapseExpand
 
siddharthshyniben profile image
Siddharth
Making wild out-on-a-limb schemes which can change the world if they work, but probably wont.

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

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Making wild out-on-a-limb schemes which can change the world if they work, but probably wont.
  • Location
    Kannur, Kerala, India
  • Joined

More fromSiddharth

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp