Open nduvalhall opened 8 months ago
It would be challenging to use the correct dictionary and also support arbitrary sides from the user because NYT does not send the whole dictionary to the client. NYT only sends the words relevant for today's puzzle.
I made a CLI version that is trimmed down to only give two word answers for today's puzzle that uses the NYT dictionary for the day.
import requests
import json
def get_todays_metadata():
r = requests.get("https://www.nytimes.com/puzzles/letter-boxed")
start_string = r.text.index("window.gameData")
start_parens = start_string + r.text[start_string:].index("{")
end_string = start_parens+ r.text[start_parens:].index("}")
todays_metadata = json.loads(r.text[start_parens:end_string]+"}")
return todays_metadata
todays_metadata = get_todays_metadata()
def two_word_solution(word_list, chars):
output = []
for word in word_list:
last = word[len(word)-1]
matches = [w for w in word_list if w[0] == last and w!= word]
for m in matches:
pair = word + m
if set(pair) == chars:
output.append([word,m])
return output
def solve_puzzle(exclude = []): # optionally exclude a list of answers
chars = set(''.join(todays_metadata['sides']))
wordset = todays_metadata['dictionary']
answers = two_word_solution(wordset, chars)
answers = [x for x in answers if x not in exclude]
return answers
if __name__ == "__main__":
print(f"⭐️ {todays_metadata['ourSolution'][0]} - {todays_metadata['ourSolution'][1]}")
for (first, second) in solve_puzzle(exclude=[todays_metadata['ourSolution']]):
print(f" {first} - {second}")
You would need to install requests
for before running this code. You can do so by running pip install requests
from the command line.
Also, thank you Alice for making this project!
I haven't tested all the words but today's letterboxed did not accept the word VARMENT but your solutions included this!