-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
100 lines (80 loc) · 2.86 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
import random
# Designing an algo to solve wordle.
def wordle(wordList):
grey, yellow, green = [], [], []
for i in range(6):
# Selecting suggestion.
if i == 0:
word = "SPARE"
else:
word = random.choice(wordList)
print("Suggested word:", word)
print("Enter your word:", end = " ")
while True:
word = input().upper()
if len(word) != 5:
print("Please enter a 5 letter word:", end = " ")
else:
break
# Obtain results.
print("Please enter the results: [X-> grey, Y-> Yellow, G-> Green]")
while True:
res = input().upper()
print()
if len(res) != 5:
print("Please enter the results correctly: [X-> grey, Y-> Yellow, G-> Green]")
else:
break
if res == "GGGGG":
print("*****************************************")
print("Hurray!!")
print("You Have Solved The Wordle!!")
print("*****************************************")
break
for i in range(5):
if res[i] == "X" and word[i] not in grey and word[i] not in green:
grey.append(word[i])
elif res[i] == "Y" and [word[i],i] not in yellow:
yellow.append([word[i],i])
elif res[i] == "G" and [word[i],i] not in green:
green.append([word[i], i])
for l,ind in green:
if l in grey:
grey.remove(l)
# Remove incorrect words.
tempList = []
for w in wordList:
flag = True
for letters in grey:
if letters in w:
flag = False
break
if flag == False:
continue
for letters, ind in yellow:
if letters not in w or w[ind] == letters:
flag = False
break
if flag == False:
continue
for letters, ind in green:
if letters not in w or w[ind] != letters:
flag = False
break
if flag:
tempList.append(w)
wordList = tempList.copy()
# Importing the text file -> Storing in a list.
fr = open("Modern word list.txt", "r")
#All valid 5 letter words
wordList = []
for elem in fr:
elem = elem.upper()
wordList.append(elem[:5])
print("*****************************************")
print("Welcome!")
print("We shall guide you in your conquest!")
print("*****************************************")
print()
wordle(wordList)
fr.close()