New York Times game “Letterbox” bookmarklet cheat

I wrote a bookmarklet that finds a two word solution to the New York Times game “Letterbox”. It worked, but for fun I decided to let an AI rewrite it to be faster. Here is the code it came up with, but it did take several tries before it could open a new tab. It runs significantly faster than what I wrote.

javascript:(function(){
    const gameData = window.gameData;
    if (!gameData?.sides || !gameData?.dictionary) return alert("Game data not found!");

    const sides = gameData.sides.join('');
    const requiredChars = {};
    for (let c of sides) requiredChars[c] = (requiredChars[c] || 0) + 1;
    
    const words = gameData.dictionary.map(w => ({ w, f: w[0], l: w.at(-1) }));
    const validCombinations = [];

    function usesAllChars(w1, w2) {
        const count = { ...requiredChars };
        for (let c of w1 + w2) if (count[c]) count[c]--;
        return !Object.values(count).some(v => v > 0);
    }

    for (let i = 0; i < words.length; i++) {
        for (let j = 0; j < words.length; j++) {
            if (i !== j && words[i].l === words[j].f && usesAllChars(words[i].w, words[j].w)) {
                validCombinations.push([words[i].w, words[j].w]);
            }
        }
    }

    validCombinations.sort((a, b) => a.join('').length - b.join('').length);

    const newTab = window.open("", "_blank");
    if (!newTab || newTab.closed) {
        alert("Pop-up blocked! Please allow pop-ups for this site.");
        return;
    }

    newTab.document.write(`<!DOCTYPE html>
        <html><head><title>Letterbox Solver</title>
        <style>body{font-family:Arial;padding:20px;}table{width:100%;border-collapse:collapse;}th,td{border:1px solid #ddd;padding:8px;text-align:left;}th{background:#f2f2f2;}</style>
        </head><body><h1>Letterbox Word Combinations</h1>
        <table><tr><th>Word 1</th><th>Word 2</th><th>Combined Length</th></tr>
        ${validCombinations.map(([w1,w2]) => `<tr><td>${w1}</td><td>${w2}</td><td>${w1.length + w2.length}</td></tr>`).join('')}
        </table></body></html>`);
    newTab.document.close();
})();

It works great. To use this, first copy the code above. Then set your browser to display the bookmark bar. Then right click on the bar and select the “new bookmark” item (I am using a chrome base browser).

Change the title, and then paste the copied code into the address field and click save.

Now when you are playing the game, you can click on this bookmarklet and it will open a new tab with all of the two word solutions to the game.

Enjoy.

Leave a Reply

Your email address will not be published. Required fields are marked *