# a quick and dirty solver for NYT's spelling bee puzzle # this file is hereby released under the MIT No Attribution License: # MIT No Attribution # Copyright 2022 Lauren Croney # Permission is hereby granted, free of charge, to any person obtaining a copy of this # software and associated documentation files (the "Software"), to deal in the Software # without restriction, including without limitation the rights to use, copy, modify, # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # version 4: performance drastically improved by using a plaintext # dictionary like the one in debian package wamerican-huge # plus, no external dependencies # of course, the caveat is this will inevitably be using a different dictionary # than the NYT, so many words it outputs will not be considered valid, and # it could even miss a couple. but this helps come up with possible solutions. # the basic rules: # words must contain at least 4 letters # words must include the center (required) letter # letters can be used more than once import sys import itertools # replace the below with your dictionary file as needed DICTIONARY = '/usr/share/dict/american-english-huge' MIN_LENGTH = 4 # minimum valid word length def get_words(chars, required, length): results = [] d = open(DICTIONARY,'r') for line in d: line = line.strip() # remove newline # ignore any words which don't contain the required lettter # or are too short if (required in line) and (len(line) >= length): match = True for char in line: if char not in chars: # ignore words with invalid letters match = False if match: results.append(line) # match found, save return results def print_usage(): print("Usage: bee LETTERS REQUIRED\n") print("LETTERS: list of all 7 letters, with no spaces") print("REQUIRED: the center (required) letter") def check_input(letters, required): valid = True # check that LETTERS and REQUIRED contain only alpha characters if (letters.isalpha() == False or required.isalpha() == False): print("Error: enter only alpha characters for LETTERS and REQUIRED\n") valid = False # check length of LETTERS and REQUIRED if (len(letters) != 7 or len(required) != 1): print("Error: enter 7 letters and 1 required letter\n") valid = False # check that REQUIRED is in LETTERS if (letters.find(required) == -1): print("Error: required letter must be in letter list\n") valid = False if (not valid): print_usage() exit(1) if __name__ == "__main__": if(len(sys.argv) != 3): print_usage() exit(1) # example: yacort letters = sys.argv[1] letters = letters.lower() # ignore case # example: y required = sys.argv[2] check_input(letters,required) words = get_words(letters,required, MIN_LENGTH) for word in words: print(word)