Problem
The New York Times recently launched some new word puzzles, one of which is Spelling Bee. In this game, seven letters are arranged in a honeycomb lattice, with one letter in the center. Here’s the lattice from December 24, 2019:
Spelling Bee screenshot, with the required letter G, and the additional letters L, A, P, X, M and E. The goal is to identify as many words that meet the following criteria:
The word must be at least four letters long.
The word must include the central letter.
The word cannot include any letter beyond the seven given letters.
Note that letters can be repeated. For example, the words GAME and AMALGAM are both acceptable words. Four-letter words are worth 1 point each, while five-letter words are worth 5 points, six-letter words are worth 6 points, seven-letter words are worth 7 points, etc. Words that use all of the seven letters in the honeycomb are known as “pangrams” and earn 7 bonus points (in addition to the points for the length of the word). So in the above example, MEGAPLEX is worth 15 points.
Which seven-letter honeycomb results in the highest possible game score? To be a valid choice of seven letters, no letter can be repeated, it must not contain the letter S (that would be too easy) and there must be at least one pangram.
For consistency, please use this word list to check your game score.2
Solution
import datetime
from collections import defaultdict
from itertools import combinations
import urllib.request
# Load all dictionary words from file
= "https://norvig.com/ngrams/enable1.txt"
DICT_URL
def load_dictionary(url):
= urllib.request.urlopen(url)
response return [l.decode('utf-8').strip() for l in response.readlines()]
= load_dictionary(DICT_URL)
ALL_WORDS
print(ALL_WORDS[0:10])
Calculate word score based on the rules below
- Four-letter words are worth 1 point each, while five-letter words are worth 5 points, six-letter words are worth 6 points, seven-letter words are worth 7 points, etc.
- Words that use all of the seven letters in the honeycomb are known as “pangrams” and earn 7 bonus points(in addition to the points for the length of the word)
def word_score(word):
= 7 if len(set(word)) == 7 else 0
bonus return 1 if len(word) == 4 else len(word) + bonus
Valid HoneyCombPuzzle words as per the rules below
- The word must be at least four letters long.
- The word must include the central letter.
- The word cannot include any letter beyond the seven given letters.
= [w for w in ALL_WORDS if len(w) >= 4 and ("s" not in w)
HCP_WORDS and len(set(w)) <= 7]
# Word scores for all HCP WORD
= {w: word_score(w) for w in HCP_WORDS}
HCP_WORD_SCORES
# The set of HPC Words for a set of letters
= defaultdict(list)
HCP_WORDSETS for w in HCP_WORDS:
frozenset(w)].append(w) HCP_WORDSETS[
Algorithm for calculating the score obtainable in a HoneyCombPuzzle
- Find all subsets using the 6 letters surrounding the central letter (this results in 64 subsets for each puzzle)
- Add the central letter to each of the subsets (as central character should be present in each word formed)
- For each subset, identify the list of HPC words that can be formed using letters of the subset
- Taking the union of all the words that can be formed by each subset, gives us all the HPC words that can be formed using the letters of the puzzle
- Sum the scores of all the HPC words for all the subsets to get the puzzle score
def letter_subsets(hc):
= hc
center, surrounding = []
subsets for r in range(1, 7):
for comb_r in combinations(surrounding, r):
= frozenset(list(comb_r)+[center])
subset
subsets.append(subset)return subsets
def score(hc):
return sum([HCP_WORD_SCORES[w] for subset in letter_subsets(hc)
for w in HCP_WORDSETS[subset]])
def solution(hc):
return [w for subset in letter_subsets(hc)
for w in HCP_WORDSETS[subset]]
def print_solution(hc):
print("HoneyCombPuzzle:", hc)
= solution(hc)
words print("Number of words:", len(words))
for w in words:
print(w, HCP_WORD_SCORES[w])
= score(hc)
sc print("Total score:", sc)
Which seven-letter honeycomb results in the highest possible game score? To be a valid choice of seven letters, no letter can be repeated, it must not contain the letter S (that would be too easy) and there must be at least one pangram.
Algorithm for identifying all valid sets of seven letter honeycombs
- Out of all valid HPC words, identify words which are formed with seven different letters (because the seven letters should form atleast one pangram)
- From each word identify the set of 7 distinct letters
- Each set of 7 distinct letters gives rise to 7 distinct honeycombs
- Identify the honeycomb with the largest score
# Words that use all of the seven letters in the honeycomb are known as “pangrams”
def best():
def valid_honeycombs():
= [set(w) for w in HCP_WORDS if len(set(w)) == 7]
pangram_sets return [(c, ls.difference(c)) for ls in pangram_sets for c in ls]
return max([(score(hc), hc) for hc in valid_honeycombs()])
if __name__ == "__main__":
'g', 'ameplx'))
print_solution((print("Start:", datetime.datetime.now())
print("The Best HoneyCombPuzzle Score: %d, Puzzle: %s" % best())
print("End:", datetime.datetime.now())