-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle1stats.py
151 lines (118 loc) · 4.43 KB
/
wordle1stats.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
"""
Computer plays wordle (random guessing).
:author: Terry Sergeant
:version: 1
In this version the computer randomly selects a word, then reduces the
list of possible solutions and randomly guesses from the remaining words.
"""
import random
import re
from colorama import Fore, Back, Style
def is_possible(word, result):
"""
Determine whether a word is possible based on guesses made so far
and the partial word provided by the user.
:param word: The word we are considering as a possible match.
:param result: A dictionary that stores the guess and the grade.
:returns: True if the word is a possible match; False otherwise.
"""
guesslist = list(result['guess'])
gradelist = list(result['grade'])
wordlist = list(word)
# if perfect match letters don't line up then its not a match
for i in range(len(word)):
if gradelist[i] == '2':
if guesslist[i] != word[i]:
return False
else:
# remove letters that are a perfect match
wordlist[i] = '!'
guesslist[i] = '@'
for i in range(len(word)):
if gradelist[i] == '1':
# right letter, wrong position -> if its there mark it out, if not
# return False. If it is in the same spot return False.
if guesslist[i] == wordlist[i]:
return False
if not (guesslist[i] in wordlist):
return False
for j in range(len(word)):
if guesslist[i] == wordlist[j]:
guesslist[i] = '@'
wordlist[j] = '!'
break
elif gradelist[i] != '*':
# we guessed wrong letter completely so it shouldn't be left in the word
if guesslist[i] in wordlist:
return False
return word != guess # true unless the word matches our guessed word
def reduce_word_list(wordlist, result):
"""
Narrow down list of possible choices.
:param wordlist: List of words that are still considered possibilities.
:param result: A dictionary that stores the guess and the grade.
:returns: Modified and shortened word_list.
"""
return [w for w in wordlist if is_possible(w, result)]
def load_dictionary(filename):
"""
Read the dictionary from the file and return the words as a list.
:param filename: The file containing the official dictionary.
:returns: wordlist
"""
wordlist = []
with open(filename) as f:
for word in f:
wordlist.append(word.rstrip())
return wordlist
def get_next_guess(wordlist):
"""
Randomly select a word from the list of words.
:param wordlist: The list of words currently being considered.
:returns: A randomly selected word from the list.
"""
return wordlist[random.randint(0,len(wordlist)-1)]
def grade_word(answer, guess):
"""
Grades the guess by comparing it to the correct answer.
:param answer: The answer word the computer is trying to guess.
:param guess: The guessed word to be graded.
:returns: Returns a dictionary containing the original guess and the grade
string.
NOTE: The grade string contains 5 characters as follows:
* there is a 2 if the correct letter is used in the correct place
* there is a 1 if the correct letter is used in the wrong place
* the original guessed letter is used if the letter does not occur in the answer
"""
answerlist= list(answer)
guesslist= list(guess)
for i in range(len(answer)):
if answerlist[i] == guesslist[i]:
guesslist[i] = '2'
answerlist[i] = 0
for i in range(len(answer)):
for j in range(len(answer)):
if answerlist[i] == guesslist[j]:
guesslist[j] = '1'
answerlist[i] = 0
return {
"guess": guess,
"grade": "".join(guesslist),
}
#-------------------------------------------------------------------------------------
random.seed()
origlist = load_dictionary("dictionary.txt")
total = 0
for answer in origlist:
wordlist= load_dictionary("dictionary.txt")
count = 0
while True:
count += 1
guess = get_next_guess(wordlist)
result = grade_word(answer, guess)
if result['grade'] == '22222':
break;
wordlist = reduce_word_list(wordlist, result)
total += count
print(answer + " " + str(count))
print (total / len(origlist))