Skip to content

Instantly share code, notes, and snippets.

@codergautam
Last active February 22, 2022 22:28
Show Gist options
  • Save codergautam/3e9221c471b52c3ea66b9b0e55754e85 to your computer and use it in GitHub Desktop.
Save codergautam/3e9221c471b52c3ea66b9b0e55754e85 to your computer and use it in GitHub Desktop.
A simple wordle solver
//go into wordle (https://www.powerlanguage.co.uk/wordle/)
//open js console (right click -> inspect -> console in top bar)
//paste the below code
//watch your wordle get solved! (100% GUARANTEED TO SOLVE, AT MOST 6 TRIES)
var guesses = eval(/var Ma=(\[.*"zymic"\])/.exec(await fetch(`main.${window.wordle.hash}.js`).then(r => r.text()))[1]); var answers = eval(/var Ma=(\[.*"shave"\])/.exec(await fetch(`main.${window.wordle.hash}.js`).then(r => r.text()))[1]);var all=[...guesses,...answers];const alphabet="abcdefghijklmnopqrstuvwxyz".split(""),matchesFilters=(t,e)=>t.filter((t=>{let r=!0;for(let o=0;o<e.length;o+=1){const{colour:s,position:n,letter:l}=e[o];if("black"===s&&t.includes(l)){r=!1;break}if("green"===s&&t[n]!==l){r=!1;break}if("yellow"===s&&(!t.includes(l)||t[n]===l)){r=!1;break}}return r})),colours=["green","yellow","black"],calculateLetterColor=(t,e,r,o)=>{const s=matchesFilters(t,[{colour:o,position:r,letter:e}]);return{p:1*s.length/t.length,list:s}},createObject=(t,e,r)=>{if(r>4)return e;{colours.forEach((o=>{!e[o]&&e.list.length>0&&(e[o]=calculateLetterColor(e.list,t[r],r,o))}));const o=r+1;colours.forEach((r=>{e.list.length>0&&createObject(t,e[r],o)}))}},fillInObject=(t,e)=>{let r={list:e,p:1};return createObject(t,r,0),r},calculateP=(t,e,r,o)=>{colours.forEach((s=>{e[s]&&e[s].list.length>0&&(4===o?t.push(e[s].p*r):calculateP(t,e[s],e[s].p*r,o+1))}))},calculateWordScore=(t,e)=>{const r=[];calculateP(r,t,1,0);return r.map((t=>t*t)).reduce(((t,e)=>t+e),0)};function test(t,e){if("notInWord"===e[0])return!t.includes(e[1]);if("notAtPos"===e[0]){const[r,o,s]=e;return t.includes(s)&&t[o]!==s}if("atPos"===e[0]){const[r,o,s]=e;return t[o]===s}return!1}function distinct(t){return[...new Set(t)]}class Solver{constructor(){this.wordlist=all,this.constraints=[]}guess(t,e){const r=[];var o="";for(let s=0;s<t.length;s++)"g"===e[s]?(r.push(["atPos",s,t[s]]),o+=t[s]):"y"===e[s]&&(r.push(["notAtPos",s,t[s]]),o+=t[s]);for(let s=0;s<t.length;s++)"b"!==e[s]||o.includes(t[s])||r.push(["notInWord",t[s]]);this.constraints=this.constraints.concat(r)}getFiltersv3(){var t=[];return this.constraints.forEach((e=>{"notInWord"==e[0]?t.push({colour:"black",letter:e[1]}):"atPos"==e[0]?t.push({colour:"green",letter:e[2],position:e[1]}):"notAtPos"==e[0]&&t.push({colour:"yellow",letter:e[2],position:e[1]})})),t}nextBestv3(){var t=this.getFiltersv3();if(0==t.length)return"raise";const e=matchesFilters(answers,[...t]);let r=answers,o=1,s=r[0];if(1===e.length)return e[0];if(e.length<4){r=e;for(let t=1;t<r.length;t+=1){const n=fillInObject(r[t],e),l=calculateWordScore(n,r[t]);l<o&&(o=l,s=r[t])}return s}var n=[];for(let t=0;t<r.length;t+=1){const s=fillInObject(r[t],e),l=calculateWordScore(s,r[t]);l<=o&&(o=l,n.push({word:r[t],score:l}))}return(n=n.filter((t=>t.score==o)))[~~(Math.random()*n.length)].word}}function getLastGuess(){var t=document.querySelector("game-app"),e=t.boardState.filter((t=>""!=t));return[e[e.length-1],t.evaluations[e.length-1].map((t=>"correct"==t?"g":"present"==t?"y":"b")).join("")]}function guess(t){return new Promise(((e,r)=>{t.split("").forEach(((t,e)=>{setTimeout((()=>{dispatchEvent(new KeyboardEvent("keydown",{key:t}))}),100*(e+1))})),setTimeout((()=>{document.querySelector("game-app").$keyboard.shadowRoot.querySelector("[data-key=↵]").click(),setTimeout((()=>{e()}),500)}),900)}))}var yo=new Solver;function go(){var t=getLastGuess();"ggggg"==t[1]?setTimeout((()=>{alert("wordle done! yay!")}),1200):(yo.guess(...t),guess(yo.nextBestv3()).then((()=>{go()})))}guess("raise").then((()=>{go()}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment