Coder Social home page Coder Social logo

spellingbee's Introduction

New York Times "Spelling Bee" puzzle solver

USAGE: spellingbee LETTERS [pivot]

![Spelling Bee example](img/Spelling Bee.png)

Architecture

I challenged myself to find a somewhat clever or optimized way of solving NYT's "Spelling Bee" daily challenge. The resulting architecture isn't the most optimal (though still rather performant) for solving a single NYT Spelling Bee puzzle, but is rather designed so that it builds a reusable map of the information needed to solve multiple Spelling Bee (or other anagram-like puzzles) upon startup, after which it can service any number of different anagram/SB requests in an optimized fashion.

Dictionary Word Factorization

At startup, the contents of the word file (currently just hard-coded to read the system dictionary from /usr/share/dict/words) are analyzed and each word is converted into its factors: the unique set of (lowercased) letters comprising the word, sorted by alphabetical order. As these factors do not by any means represent a unique fingerprint for each word and can repeat, the factors are deduplicated and words sharing factors are collapsed together.

A map between each of these unique factors and the words sharing the same factor (essentially, HashMap<Factor, Vec<Word>>) is generated in this process, and the map is persisted for the lifetime of the application. Until this point, nothing to do with the current puzzle has been taken into consideration and all the words in the dictionary have been factored.

Servicing Anagram Puzzles

To solve a NYT Spelling Bee challenge (or any other anagram puzzle), the letters available for use are provided to the application (currently, via the command line at startup, but you can imagine a web page servicing these requests as well).

We generate a set of all possible combinations (not permutations) of the input letters, for example so that when given a unique set of letters { A, B, C }, we can find words with any of the factors { A }, { B }, { C }, { A, B }, { A, C }, { B, C }, { A, B, C }. Each of these "subfactors" is looked up in the factor map, and from there we get a list of words matching each combination subfactor. For NYT Spelling Bee puzzles, there's the additional constraint that the central letter (hereafter called the "pivot") must be present in all solutions, so we drop or skip any subfactors that don't contain this pivot letter.

Now, armed with our map of all factors to dictionary words and a set of all possible combinations meeting our requirements that can be formed from the input letters, we just look up each of the subfactors in the list and add the resulting list of words to our list of correct answers.

License, Copyright, and Authorship

This code is released to the public under the terms of the MIT license. This project and documentation were written by Mahmoud Al-Qudsi. Any rights not conferred by the license are reserved under copyright.

spellingbee's People

Contributors

mqudsi avatar

Stargazers

GAURAV avatar

Watchers

 avatar

Forkers

icodein

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.