-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
140 lines (120 loc) · 4.04 KB
/
solver.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
import numpy as np
import json
def check_double(grey_list, yellow_list, green_list, doubles=[]):
for i in green_list:
for j in grey_list:
if i[0] == j[0]:
doubles.append(j)
grey_list.remove(j)
for i in yellow_list:
for j in grey_list:
if i[0] == j[0]:
doubles.append(j)
grey_list.remove(j)
return grey_list, doubles
def check_yellow(word, yellow_list):
prime = False
if len(yellow_list) == 0:
return True
for y in yellow_list:
if word.find(y[0]) != -1 and word[y[1]] != y[0]:
prime = True
else:
prime=False
break
return prime
def check_grey(word, only_greys, grey_list, doubles):
if len(grey_list) == 0:
return False
for i in range(0, len(word)):
if (word[i], i) in doubles:
return True
if word[i] in only_greys:
return True
else:
return False
def check_green(word, green_list):
prime = False
if len(green_list) == 0:
return True
for g in green_list:
if word[g[1]] == g[0]:
prime = True
else:
prime = False
break
return prime
def solver(letters, num):
file = open("dictionary.json")
data = json.load(file)
data = list(data.keys())
x = [i for i in data if len(i) == 5]
letters = letters[:num] #do this only when wordle is already completed
#letters.append([('p', "green"), ('a', "green"), ('s', "grey"), ('s', 'green'), ('e', 'green')])
print(len(x))
grey_list = list(set([(j[0], i.index(j)) for i in letters for j in i if j[1] == "grey"]))
yellow_list = list(set([(j[0], i.index(j)) for i in letters for j in i if j[1] == "yellow"]))
green_list = list(set([(j[0], i.index(j)) for i in letters for j in i if j[1] == "green"]))
grey_list, doubles = check_double(grey_list, yellow_list, green_list)
only_greys = [i[0] for i in grey_list]
"""
print("***********")
print(doubles)
print(only_greys)
print("***********")
print(grey_list)
print(yellow_list)
print(green_list)"""
short = {} #consists of all possible words
finalised = ["0", "0", "0", "0", "0"]
for i in x:
if not(check_grey(i, only_greys, grey_list, doubles)):
if check_yellow(i, yellow_list):
if check_green(i, green_list):
short[i] = 0
for i in green_list:
finalised[i[1]] = i[0]
let_dict = {}
prime = False
for i in short:
for j in range(0, len(i)):
for g in green_list:
if g[0] == i[j] and g[1] == j:
prime = True
if prime == False:
if i[j] not in let_dict:
let_dict[i[j]] = 1
else:
let_dict[i[j]] += 1
prime=False
let_dict = dict(sorted(let_dict.items(), key=lambda item: item[1])) #all letters
#print(short)
#print(finalised)
#print(let_dict)
word_count = []
for i in range(0, len(finalised)):
if finalised[i] == "0":
for let in let_dict:
for word in short:
if word[i] == let:
word_count.append((let, i))
#print(let, word)
temp_dict = {}
for i in list(set(word_count)):
print(i, word_count.count(i))
key = i[0] + str(i[1])
temp_dict[key] = word_count.count(i)
print(temp_dict)
score_words = {}
for word in short:
total = 0
splitted_word = list(word)
for i in range(0, len(splitted_word)):
#print((splitted_word[i] + str(i)))
if (splitted_word[i] + str(i)) in temp_dict:
total += temp_dict[splitted_word[i] + str(i)]
short[word] = total
short = dict(sorted(short.items(), key=lambda item: item[1]))
print()
print()
print(short)