-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordleSolver.py
More file actions
executable file
·161 lines (141 loc) · 6.82 KB
/
Copy pathwordleSolver.py
File metadata and controls
executable file
·161 lines (141 loc) · 6.82 KB
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
#!/usr/bin/env python3
# While unsolved:
# Determine what word to choose
# Running total
# For each word in scoreDict:
# clear presentLetters
# For each letter in word:
# presentLetters.Pushback each non-duplicate letter (use a set)
# For each letter in presentLetters:
# Increment wordLetters by letter
# Scoring the words
# For each word in scoreDict:
# # For each letter in word:
# presentLetters.Pushback each non-duplicate letter (use a set)
# For each letter in presentLetters:
# letterScore = wordLetters[letter]
# scoreDict[word][0] += letterScore
# Sort
# Pick the greatest one from scoreDict
# Reset the scoreDict if the word managed to go through
# Read what the colors of the word were
# Update the list of possible words
import re
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# I N I T I A L I Z A T I O N
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
introString = """\n\n\n\n\n\n\n\n\n\n\n
██╗ ██╗ ██████╗ ██████╗ ██████╗ ██╗ ███████╗
██║ ██║██╔═══██╗██╔══██╗██╔══██╗██║ ██╔════╝
██║ █╗ ██║██║ ██║██████╔╝██║ ██║██║ █████╗
██║███╗██║██║ ██║██╔══██╗██║ ██║██║ ██╔══╝
╚███╔███╔╝╚██████╔╝██║ ██║██████╔╝███████╗███████╗
╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚══════╝╚══════╝
███████╗ ██████╗ ██╗ ██╗ ██╗███████╗██████╗
██╔════╝██╔═══██╗██║ ██║ ██║██╔════╝██╔══██╗
███████╗██║ ██║██║ ██║ ██║█████╗ ██████╔╝
╚════██║██║ ██║██║ ╚██╗ ██╔╝██╔══╝ ██╔══██╗
███████║╚██████╔╝███████╗╚████╔╝ ███████╗██║ ██║
╚══════╝ ╚═════╝ ╚══════╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝
"""
print(introString)
# Layout is like this:
# "Word": [score, percent chance of being in wordle]
scoreDict = {}
goodWords = open("wordleWords").read().splitlines()
for line in goodWords:
lineElements = line.split()
scoreDict[lineElements[0]] = [0, float(lineElements[1].strip('%'))]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# D E T E R M I N E W H A T W O R D T O C H O O S E
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wordFound = False
for i in range(6):
# Running total
# For each word in scoreDict:
# For each letter in word:
# presentLetters.Pushback each non-duplicate letter (use a set)
# For each letter in presentLetters:
# Increment wordLetters by letter
wordLetters = {chr(c): 0 for c in range(ord('A'), ord('Z') + 1)} # Dictionary of characters A-Z
presentLetters = set()
for word in scoreDict:
presentLetters.clear()
for letter in word:
presentLetters.add(letter)
for letter in presentLetters:
wordLetters[letter] += 1
# print (wordLetters)
# Scoring the words
# For each word in scoreDict:
# # For each letter in word:
# presentLetters.Pushback each non-duplicate letter (use a set)
# For each letter in presentLetters:
# letterScore = wordLetters[letter]
# scoreDict[word][0] += letterScore
for word in scoreDict:
presentLetters.clear()
for letter in word:
presentLetters.add(letter)
for letter in presentLetters:
letterScore = wordLetters[letter]
scoreDict[word][0] += letterScore
# Sorting the results
sortedScores = sorted(scoreDict.items(), key=lambda item: item[1][0], reverse=True)
# Choosing a word to present to the user
for recommendedItem in sortedScores:
recommendedWord = recommendedItem[0]
recommendedScore = recommendedItem[1][0]
recommendedPercent = recommendedItem[1][1]
while True: # Nested for loop to let us keep printing sortedScores without iterating to the next item
print(f"I recommend this word: {recommendedWord}, score of {recommendedPercent}")
colors = input("Tell me the colors of the letters in order (like 'bbgyb'): ")
if colors == 'p':
print(sortedScores)
else:
break
if colors == 'n': # User wants to see the next recommended word
continue
if len(colors) == 5 and set(colors).issubset({'g', 'b', 'y'}): # User entered colors
break;
else: # User entered something that wasn't recognized
break
if colors == 'ggggg' or colors == 'q': # We solved the puzzle or the user just wants to stop the whole program
break
# Count green and yellow occurrences for each letter in the guess
from collections import Counter
guess = recommendedWord
green_yellow_counts = Counter()
for i, c in enumerate(colors):
if c in ('g', 'y'):
green_yellow_counts[guess[i]] += 1
toRemove = set()
for word in scoreDict:
remove = False
word_counter = Counter(word)
# First, check green and yellow rules
for i, c in enumerate(colors):
l = guess[i]
if c == 'g':
if word[i] != l:
remove = True
break
elif c == 'y':
if word[i] == l or l not in word:
remove = True
break
# Now, check black rules
for i, c in enumerate(colors):
l = guess[i]
if c == 'b':
allowed = green_yellow_counts[l]
if word_counter[l] > allowed:
remove = True
break
if remove:
toRemove.add(word)
for word in toRemove:
del scoreDict[word]
# Reset scoreDict scores (but keep the remaining words)
for word in scoreDict:
scoreDict[word][0] = 0