I recently started playing Wordle, and while I love the game, I kind of got annoyed when I had guessed a couple of words and had only a few left, but had to go through the letters in my head one by one trying to figure out what was possible. Brute-forcing letters like that is the job of a computer.... So I made a program to help me figure out possible letters more quickly.
Basically, I took all the 5-letter English words, and knock them down based on what is known to be missing, what is known to be there, what is where, and what is not where. I hope that someone else enjoys this program.
#!/usr/bin/python3
'''
This script reads in a dictioary of 5-letter english (supposedly) words (I AWK'd the five-letter words from a dictionary that may include non-real words),
and prints out words that remain after the User Defined Variables are entered
Enter User Defined Variables with single quotes, separated by commas (i.e. 'o' or ['t','y'])
- First enter characters that are known in the correct position
- Next, enter characters that are known to be in the word (but not in the correct position)
- Next, enter characters that are known NOT to be in the word
- Last, Enter the characters that are known not to be in the correct position
'''
#dictionary_file = open('/home/john/Downloads/Five-Letter-English_Dictionary_Wordlist.txt', 'r')
dictionary_file = open('/home/john/valid-wordle-words.txt', 'r')
#dictionary_file = open('/home/john/Valid-Wordle-Words.txt', 'r')
Lines = dictionary_file.readlines()
############################################################
##### Begin User Defined Variable Section ##############
FirstIs = 's'
SecondIs = ''
ThirdIs = ''
FourthIs = ''
FifthIs = 'e'
IsInWord = ['n','i']
IsNotInWord = ['p','l','a','t','o','r']
FirstIsNot = ['']
SecondIsNot = ['n']
ThirdIsNot = ['i']
FourthIsNot = ['n']
FifthIsNot = ['']
##### End User Defined Variable Section ##############
#########################################################
TempFile = []
TempFile2 = []
############### Pocess the letters that are known to be in a place
###### Process First Letter ##############
if FirstIs != '': # Process First Letter
for Line in Lines:
if FirstIs == Line[0]:
TempFile.append(Line.strip())
else:
for Line in Lines:
TempFile.append(Line.strip())# Skip First Letter and move along
###### Process Second Letter ##############
if SecondIs != '': # Process Second Letter
for item in TempFile:
if SecondIs == item[1]:
TempFile2.append(item.strip())
TempFile = TempFile2
TempFile2 = []
###### Process Third Letter ##############
if ThirdIs != '': # Process Third Letter
for item in TempFile:
if ThirdIs == item[2]:
TempFile2.append(item.strip())
TempFile = TempFile2
TempFile2 = []
###### Process Fourth Letter ##############
if FourthIs != '': # Process Fourth Letter
for item in TempFile:
if FourthIs == item[3]:
TempFile2.append(item.strip())
TempFile = TempFile2
TempFile2 = []
###### Process Fifth Letter ##############
if FifthIs != '': # Process Fifth Letter
for item in TempFile:
if FifthIs == item[4]:
TempFile2.append(item.strip())
TempFile = TempFile2
TempFile2 = []
############### Pocess the letters that are known NOT to be in a place
###### Process First Letter ##############
if FirstIsNot != ['']: # Process First Letter
for thing in FirstIsNot:
for item in TempFile:
if thing != item[0]:
TempFile2.append(item.strip())
TempFile = TempFile2
TempFile2 = []
###### Process Second Letter ##############
if SecondIsNot != ['']: # Process Second Letter
for thing in SecondIsNot:
for item in TempFile:
if thing != item[1]:
TempFile2.append(item.strip())
TempFile = TempFile2
TempFile2 = []
###### Process Third Letter ##############
if ThirdIsNot != ['']: # Process Third Letter
for thing in ThirdIsNot:
for item in TempFile:
if thing != item[2]:
TempFile2.append(item.strip())
TempFile = TempFile2
TempFile2 = []
###### Process Fourth Letter ##############
if FourthIsNot != ['']: # Process Fourth Letter
for thing in FourthIsNot:
for item in TempFile:
if thing != item[3]:
TempFile2.append(item.strip())
TempFile = TempFile2
TempFile2 = []
###### Process Fifth Letter ##############
if FifthIsNot != ['']: # Process Fifth Letter
for thing in FifthIsNot:
for item in TempFile:
if thing != item[4]:
TempFile2.append(item.strip())
TempFile = TempFile2
TempFile2 = []
####### Process Known Inclusive Letters ######################
if IsInWord != ['']:
for useme in IsInWord:
for item in TempFile:
if useme in item:
TempFile2.append(item.strip())
TempFile = TempFile2
TempFile2 = []
####### Process Known Absent Letters ######################
if IsNotInWord != ['']:
for removeme in IsNotInWord:
for item in TempFile:
if removeme not in item:
TempFile2.append(item.strip())
TempFile = TempFile2
TempFile2 = []
#print("Here are the words left to choose from:")
#for item in TempFile:
# print(item.strip()) # Print out the remaining words
############################## Print Letters and how often they occur (Letter Frequency Table) #########################################
LetterStats = []
for item in TempFile:
for letter in item:
#print(letter)
if letter in LetterStats:
#print("letter: " + letter + " is in the list... incrimenting count")
letterindex = LetterStats.index(letter)
LetterStats[letterindex + 1]= LetterStats[letterindex + 1] +1
else:
#print("letter: " + letter + " is not in the list... adding....")
LetterStats.append(letter)
LetterStats.append(1)
#print("Here are the letter stats ", LetterStats)
SortedLetterList = []
#print("Length of LetterStats is: " + str(len(LetterStats)))
for i in range(0, len(LetterStats)-1,2):
#print(LetterStats[i])
SortedLetterList.append((LetterStats[i],int(LetterStats[i+1])))
#print("List before sorting: ")
#print(SortedList)
SortedLetterList.sort(key=lambda a: a[1], reverse=True)
print("Letter Frequency Table is: ")
print(SortedLetterList)
print(" ") #skip a linne
############################# Next up is to give each word a score... If it has an e, it has the number of times e is seen in the remaining word list added to the score....
############################# if it has a t, it has the number of times t is seen added to the score.
############################# It then sorts the words from highest value to lowest... this will give the 'best' words to pick.
############################# 'best' means it will decrease the remaining words the fastest. It is assumed that any word left is as likely as any other to be chosen by Wordle
ScoredWordList = []
for item1 in TempFile: #Iterate through the words left
#print("Iterating through letters of word ", item1)
ScoredWordList.append(item1)
ScoredWordList.append(0)
for i in range(0, len(SortedLetterList)):
#print("Checking to see if ", SortedLetterList[i][0], " is in the word ", item1)
if SortedLetterList[i][0] in item1: #The letter is in the word
#print(item2[0], "is in word ", item1, ". Adding: ", item2[1], " to the score of ", item1)
ScoredWordList[-1] = ScoredWordList[-1] + SortedLetterList[i][1] # Add to the value of the word
#SortedWordList[:-1] = SortedWordList[:-1] + SortedLetterList[wordscore+1]
#print("This is the Word List with scores\n", ScoredWordList)
SortedScoredWordList = []
for i in range(0, len(ScoredWordList), 2):
SortedScoredWordList.append((ScoredWordList[i],int(ScoredWordList[i+1])))
SortedScoredWordList.sort(key=lambda a: a[1], reverse=True)
print("The best guessing order is:")
for i in SortedScoredWordList:
print(i)
Comments
Post a Comment