-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
190 lines (138 loc) · 5.71 KB
/
Main.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
import json
from functools import wraps
import hashlib
import multiprocessing
from multiprocessing import Pool
from ProgressBar import ProgressBar
def memoize(obj):
cache = obj.cache = {}
@wraps(obj)
def memoizer(*args, **kwargs):
key = str(args) + str(kwargs)
if key not in cache:
cache[key] = obj(*args, **kwargs)
return cache[key]
return memoizer
def GetHash(Input):
return hashlib.sha256(str(Input).encode('utf-8')).hexdigest()
def GetPreCompute(Hash):
try:
File=open(f"PreComputed/{Hash}.txt","r")
FC=json.load(File)
File.close()
return FC
except:
return False
def SavePreCompute(Hash,Word,Groups):
File=open(f"PreComputed/{Hash}.txt","w")
json.dump({"Word":Word,"Groups":Groups},File)
File.close()
@memoize
def ComputeCode(Input,Correct):
Output = ["0","0","0","0","0"]
Check=list(Correct)
for X,Y in enumerate(Input):
if Y == Correct[X]:
Check.remove(Y)
Output[X]="2"
for X,Y in enumerate(Input):
if Y in Check and Y != Correct[X]:
Check.remove(Y)
Output[X]="1"
return ["".join(Output),Correct]
class BotClass:
def __init__(self,AllWords,Words) -> None:
self.AllWords = AllWords
self.Trimmed = Words
def GenerateGroups(self,Word=None):
BestWord=""
BestGroups={}
TrimmedHash=GetHash(self.Trimmed)
PreComputed=GetPreCompute(TrimmedHash)
if PreComputed == False or Word != None:
BestGroupScore=0
Bar=ProgressBar(28,0,len(self.AllWords),Name="Computing Best Guess")
for Count,X in enumerate(self.AllWords):
if Word == None or X == Word.lower():
#print(X)
Groups={}
with Pool(processes=4) as pool:
for Code in pool.starmap(ComputeCode,[(X,Y) for Y in self.Trimmed],chunksize=700):
#print(Code)
if Groups.get(Code[0]) == None:
Groups[Code[0]]=[]
Groups[Code[0]].append(Code[1])
Bar.Update(Count)
Trimed=set(Groups.keys())
#print(Trimed)
Total=(len(Trimed) * 4)# + (sum([243 - len(Groups[X]) for X in Trimed]) * 3) + (len([X for X in Groups if len(Groups[X]) == 1]) / len(Groups)) * 500
#Total=abs((len(self.Trimmed) / len(Trimed)) - (sum([list(Groups.keys()).count(X) for X in Trimed]) / len(Trimed)))
if Total > BestGroupScore:
BestGroupScore=Total
BestWord=X
BestGroups=Groups
if Word == None:
SavePreCompute(TrimmedHash,BestWord,BestGroups)
Bar.EndProgressBar()
else:
BestWord=PreComputed["Word"]
BestGroups=PreComputed["Groups"]
return BestWord,BestGroups
def ComputeAll(AllWords,Words):
All=0
for X in Words:
Bot=BotClass(AllWords,Words)
Guesses=1
while True:
Guesses+=1
BestGuess,BestGroups=Bot.GenerateGroups()
#print(BestGuess,Guesses,X,len(Bot.Trimmed))
Input=ComputeCode(BestGuess,X)[0]
Bot.Trimmed=BestGroups[Input]
if len(Bot.Trimmed) == 1:
#print(Bot.Trimmed)
if Bot.Trimmed[0] != X:
print("thats not good")
All+=Guesses
#exit()
break
print(All / len(Words))
print(len(Words))
if __name__ == "__main__":
while True:
AllWords = json.load(open("AllWords.txt", "r"))
Words = json.load(open("Words.txt", "r"))
#ComputeAll(AllWords,Words)
# exit()
Bot=BotClass(AllWords,Words)
StartWord=None
while True:
Input=input("Inital Guess(Leave blank for it to compute one): ").lower()
if Input == "":
break
if Input.lower().strip() in Bot.AllWords:
StartWord=Input
break
BestGuess,BestGroups=Bot.GenerateGroups(StartWord)
while True:
CurrentHash=GetHash(Bot.Trimmed)
Chance=(len([X for X in BestGroups if len(BestGroups[X]) == 1]) / len(BestGroups)) * 100
print("\n==================================================")
print("Best Guess:",BestGuess)
print("Remaining Words:",len(Bot.Trimmed))
print("Number of groups:",len(BestGroups))
print("Current Hash:",CurrentHash)
print("Chance of geting answer:","{:.3f}%".format(Chance))
print("==================================================")
Input=""
while 1:
Input=input("Enter Code: ").strip()
if len(Input) == 5 and all(T.isdigit() for T in Input) and Input in list(BestGroups.keys()):
break
Bot.Trimmed=BestGroups[Input]
if len(Bot.Trimmed) == 1:
print("The word is:",Bot.Trimmed[0])
input("\nPress Enter to continue")
break
#Bot.Trimmed=Bot.Trimmed.remove(BestGuess)
BestGuess,BestGroups=Bot.GenerateGroups()