-
Notifications
You must be signed in to change notification settings - Fork 0
/
myBestWord.py
229 lines (179 loc) · 5.78 KB
/
myBestWord.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#---------------------------------------------------------------#
# Best Starting Word
# version 1.0
# David McPhee
# written: Feb-4 2022
#---------------------------------------------------------------#
import sys
import time
import copy
from myAutoWord import *
# File Functions
def printSlow(str):
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.01)
def getDefaultDic():
return({'a': [0,0,0,0,0],
'b': [0,0,0,0,0],
'c': [0,0,0,0,0],
'd': [0,0,0,0,0],
'e': [0,0,0,0,0],
'f': [0,0,0,0,0],
'g': [0,0,0,0,0],
'h': [0,0,0,0,0],
'i': [0,0,0,0,0],
'j': [0,0,0,0,0],
'k': [0,0,0,0,0],
'l': [0,0,0,0,0],
'm': [0,0,0,0,0],
'n': [0,0,0,0,0],
'o': [0,0,0,0,0],
'p': [0,0,0,0,0],
'q': [0,0,0,0,0],
'r': [0,0,0,0,0],
's': [0,0,0,0,0],
't': [0,0,0,0,0],
'u': [0,0,0,0,0],
'v': [0,0,0,0,0],
'w': [0,0,0,0,0],
'x': [0,0,0,0,0],
'y': [0,0,0,0,0],
'z': [0,0,0,0,0]})
def OpenFile(filename):
with open(filename, "r") as abr:
return(abr.read().split(','),abr)
def CloseFile(abr):
abr.close()
def getGuess(attempt, lines):
## initialize values
freq = getDefaultDic()
score = {}
highestScore = 0
highestWord = ""
## Determine Letter Freq
for word in lines:
ones = [1,1,1,1,1]
word = word[1:6]
for place in range(len(word)):
freq[word[place]][place] += 1
## Rank Each word
# meth 1: score = sum of % of time a letter appears
## for word in lines:
## word = word[1:6]
## score[word] = 0
## letterIndex = 0
##
## # assign score
## for letter in word:
## score[word] += freq[letter][place]/len(lines)
## if word.count(letter) > 1:
## score[word] /= (word.count(letter) + 1)
##
## letterIndex += 1
##
## # get highest score
## if score[word] > highestScore:
## highestScore = score[word]
## highestWord = word
# meth 2: score = sum of % of time a letter appears
for word in lines:
word = word[1:6]
score[word] = 0
letterIndex = 0
# assign score
for letter in word:
score[word] += sum(freq[letter])/(5*len(lines))
if word.count(letter) > 1:
score[word] /= (word.count(letter))*4
letterIndex += 1
# get highest score
if score[word] > highestScore:
highestScore = score[word]
highestWord = word
return highestWord
def modList(prevGuess,result,lines):
newList = []
blackLetters = []
blackList = []
greenLetters = []
greenList = []
yellowLetters = []
yellowList = []
## Fill color lists
for place in range(len(prevGuess)):
if result[place] == "G":
greenList.append((prevGuess[place],place))
greenLetters.append(prevGuess[place])
elif result[place] == "Y":
yellowList.append((prevGuess[place],place))
yellowLetters.append(prevGuess[place])
elif result[place] == "B":
blackList.append((prevGuess[place],place))
blackLetters.append(prevGuess[place])
## Create shorter list
for word in lines:
word = word[1:6]
place = [0,1,2,3,4]
wordList = tuple(zip(word, place))
remove = False
for greenCount in range(len(greenList)):
if greenList[greenCount] not in wordList:
remove = True
for yellowCount in range(len(yellowList)):
if yellowList[yellowCount] in wordList:
remove = True
if yellowList[yellowCount][0] not in word:
remove = True
for blackCount in range(len(blackList)):
if ((blackList[blackCount][0] not in yellowLetters) and
(blackList[blackCount][0] not in greenLetters) and
(blackList[blackCount][0] in word)):
remove = True
if blackList[blackCount] in wordList:
remove = True
if not remove:
newList.append('"'+word+'"')
return newList
## Terminal
lines, ml = OpenFile("myList.txt")
CloseFile(ml)
metaLines, ml = OpenFile("myMeta.txt")
CloseFile(ml)
startingLines, ml = OpenFile("myStart.txt")
CloseFile(ml)
bestScore = 100
bestWord = ""
timer = 100
for startingWord in startingLines:
startingWord = startingWord[1:6]
totScore = 0
count = 0
for ans in metaLines:
ans = ans[1:6]
score = 7
for attempt in range(1,7):
if attempt == 1:
guess = startingWord.lower()
result = getResult(guess,ans)
shortLines = lines
else:
shortLines = modList(guess,result,shortLines)
guess = (getGuess(attempt,shortLines))
result = getResult(guess,ans)
if result == "GGGGG":
score = attempt
break
totScore += score
if timer == 0:
timer = 100
print("...".format(bestWord))
else:
timer -= 1
totScore /= len(metaLines)
if totScore < bestScore:
bestScore = totScore
bestWord = startingWord
print("Best Word: {}",format(bestWord))
print("Best Score: {}",format(bestScore))