-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplit_wordle_test.py
150 lines (128 loc) · 3.21 KB
/
replit_wordle_test.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
import random
import math
import time
import json
class Pattern:
def __init__(self):
self.falseLetters = set()
self.unfixedLetters = {}
self.fixedLetters = {}
def StringToDict(word):
out = {}
for i in range(5):
out[i] = word[i]
return out
def DictToString(word):
out = ""
for i in range(5):
out += word[i]
return out
def LetterInWord(letter, word):
for i in word:
if letter == i:
return True
return False
def LetterInWordIndex(letter, word):
for i in word:
if word[i] == letter:
return i
return False
def LetterInDict(letter, word):
for i in word:
if word[i] == letter:
return True
return False
def MatchingWord(word, pattern):
fixedLetters = pattern.fixedLetters.copy()
unfixedLetters = pattern.unfixedLetters.copy()
falseLetters = pattern.falseLetters.copy()
word = StringToDict(word)
keys = []
##green
for i in fixedLetters:
keys.append(i)
for i in keys:
if word[i] != fixedLetters[i]:
return False
word.pop(i)
fixedLetters.pop(i)
keys.clear()
##yellow
for i in unfixedLetters:
keys.append(i)
for i in keys:
if (
word[i] == unfixedLetters[i]
or LetterInDict(unfixedLetters[i], word) == False
):
return False
word.pop(i)
unfixedLetters.pop(i)
##grey
for letter in falseLetters:
if LetterInDict(letter, word) == True:
return False
return True
def NewPossibleWords(pattern, possibleWords):
newWords = []
for i in possibleWords:
if MatchingWord(i, pattern) == True:
newWords.append(i)
return newWords
def Entropy(word, wordSet, wordMatches):
probabilityDist = {}
expectedInformation = 0
size = len(wordSet)
for i in wordSet:
pattern = wordMatches[i]
if pattern in probabilityDist:
probabilityDist[pattern] += 1
else:
probabilityDist[pattern] = 1
for i in probabilityDist:
n = probabilityDist[i]
if n != 0:
p = n / size
expectedInformation += p * -math.log2(p)
return expectedInformation
def Match(guess, answer):
out = {}
answer = StringToDict(answer)
guess = StringToDict(guess)
keys = []
##green
for i in guess:
keys.append(i)
for i in keys:
if answer[i] == guess[i]:
out[i] = "0"
answer.pop(i)
guess.pop(i)
keys.clear()
##yellow
for i in guess:
keys.append(i)
for i in keys:
if LetterInDict(guess[i], answer):
out[i] = "1"
guess.pop(i)
answer.pop(i)
##grey
for i in guess:
out[i] = "2"
return DictToString(out)
def CheckGuess(guess, answer):
pattern = Pattern()
out = {}
answer = StringToDict(answer)
guess = StringToDict(guess)
keys = []
##green
for i in guess:
keys.append(i)
for i in keys:
if answer[i] == guess[i]:
out[i] = "🟩"
pattern.fixedLetters[i] = answer[i]
answer.pop(i)
guess.pop(i)