-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
executable file
·335 lines (291 loc) · 10.3 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python3
"""Wordle Solver
# Usage example:
We know we need "r", but not in the 1st position. We also need "l",
but not in the 5th position. We have already established that "o" is
in position 2 and "a" is in position 4.
$ ./solver.py -1 r -5 l -a udiceny -s _o_a_
=== 2 ===
molar
polar
solar
$
From the results, we can see the answer must be one of these
options. All candidates contain 2 unique vowels.
"""
import argparse
import os
import string
import sys
from pathlib import Path
DICT_FILE = "share/dict.lst"
VOWELS = "aeiou"
WORD_LENGTH = 5
class CharacterTracker:
"""Character tracking class"""
def __init__(self, absent=None):
# Tuples are used to ensure values cannot be modified outside
# of getter and setter methods defined here.
self._possible = tuple(string.ascii_lowercase)
self._impossible = tuple()
if absent:
self.impossible = absent
def _set_possible(self, char_list=None, possible=True):
"""Set self._possible and self._impossible based on list contents"""
attrs = ["_possible", "_impossible"]
if not possible:
attrs = attrs[::-1]
setattr(self, attrs[0], tuple(sorted(char_list)))
setattr(
self,
attrs[1],
tuple(
sorted(
[x for x in string.ascii_lowercase if x not in char_list]
)
),
)
@property
def possible(self):
"""Get a tuple of characters known to be possible"""
return self._possible
@possible.setter
def possible(self, char_list):
"""Set characters we know are possible"""
self._set_possible(char_list)
@property
def impossible(self):
"""Get a tuple of characters known to be impossible"""
return self._impossible
@impossible.setter
def impossible(self, char_list):
"""Set characters we know are impossible"""
self._set_possible(char_list, False)
class Word(str):
"""Word class"""
vowels = VOWELS
@property
def get_unique_vowel_count(self):
"""Return a count of unique vowels in the word"""
vowel_count = 0
for vowel in self.vowels:
if vowel in self.__str__():
vowel_count += 1
return vowel_count
def get_vowels(self):
"""Return a list of unique vowels in the word"""
vowels_found = []
for vowel in self.vowels:
if vowel in self.__str__():
vowels_found.append(vowel)
return vowels_found
class Dictionary:
"""Dictionary class"""
def __init__(self, dict_file=None):
self.words = []
if dict_file:
self.import_dict(dict_file)
def import_dict(self, dict_file):
"""Import words from a dictionary file"""
with open(dict_file, "r", encoding="utf-8") as file_handler:
while line := file_handler.readline().rstrip():
try:
word = line.split("\t")[0].lower()
except IndexError:
continue
else:
if len(word) == WORD_LENGTH:
for word_char in word:
if word_char not in string.ascii_lowercase:
break
else:
self.words.append(Word(word))
def get_words_ordered_by_vowel_count(
self, exclude_words_with_chars=None, vowel_count=len(VOWELS)
):
"""Get a list of words ordered by highest vowel count"""
counter = vowel_count
results = []
if not exclude_words_with_chars:
exclude_words_with_chars = ""
while counter >= 0:
for word in [
w
for w in self.words
if len(w.get_vowels()) == counter
and not any(x in w for x in exclude_words_with_chars)
]:
results.append(word)
counter -= 1
return results
def get_character_stats(self, exclude_words_with_chars=None):
"""Return characters and their stats"""
stats = {}
for word in [
w
for w in self.words
if not any(x in w for x in exclude_words_with_chars)
]:
for character in word:
stats.setdefault(character, {"word_count": 0})[
"word_count"
] += 1
return stats
class KnownWordle:
"""Manage known word data"""
def __init__(self, dictionary, known):
self.dictionary = dictionary
absent_chars = list(known.get("absent") or "") or None
self.wordle_chars = CharacterTracker(absent=absent_chars)
self.wrong_spot = {}
for i in range(1, WORD_LENGTH + 1):
self.wrong_spot[i - 1] = list(known.get(f"wrong_spot_{i}") or "")
self.solved = ["_"] * WORD_LENGTH
if known.get("solved"):
if len(known["solved"]) != WORD_LENGTH:
raise ValueError(f"`solved` str length != {WORD_LENGTH}")
self.solved = list(known["solved"])
def wrong_spot_characters(self, word):
"""Check a word has all "wrong_spot" characters in other positions"""
needed_characters = sorted(
[y for x in self.wrong_spot.values() for y in x]
+ [x for x in self.solved if x != "_"]
)
for i, characters in self.wrong_spot.items():
for character in characters:
total_char_occurances = needed_characters.count(character)
if (
word.count(character) < total_char_occurances
or word[i] == character
):
return False
return True
def get_candidates_ordered_by_vowel_count(self):
"""Get a list of word candidates ordered by unique vowels"""
results = []
for word in self.dictionary.get_words_ordered_by_vowel_count(
self.wordle_chars.impossible
):
if self.wrong_spot_characters(word):
for i, word_char in enumerate(self.solved):
if word_char not in ("_", word[i]):
break
else:
results.append(word)
return results
def parse_args():
"""Parse CLI arguments"""
parser = argparse.ArgumentParser(description="Help solve Wordle puzzles.")
parser.add_argument(
"-a",
"--absent",
metavar="CHARS",
help="Characters known to be absent from the solution",
)
for i in range(1, WORD_LENGTH + 1):
parser.add_argument(
f"-{i}",
f"--wrong-spot-{i}",
metavar="CHARS",
help=(
"Characters known to exist in the word but are not solved or "
f"in position {i}"
),
)
parser.add_argument(
"-s",
"--solved",
metavar="CHARS",
help=(
f"{WORD_LENGTH} character string representing solved part "
"of the answer (replacing remaining unknowns with an "
"underscore character)"
),
)
args = parser.parse_args()
if "absent" in args and args.absent:
if not args.absent.isalpha():
parser.error(
"Given string of absent characters is not entirely alphabetic"
)
for i in range(1, WORD_LENGTH + 1):
if f"wrong_spot_{i}" in args and getattr(args, f"wrong_spot_{i}"):
if not getattr(args, f"wrong_spot_{i}").isalpha():
parser.error(
f"Given characters for Wordle character {i} are not "
"entirely alphabetic"
)
if "solved" in args and args.solved:
if len(args.solved) != WORD_LENGTH:
parser.error(
f"Given solved string length is not {WORD_LENGTH} characters"
)
if not (
(args.solved == "_" * WORD_LENGTH)
or args.solved.replace("_", "").isalpha()
):
parser.error(
"Given solved string is not entirely alphabetic (excluding "
"underscores)"
)
total_wrong_spot = 0
for i in range(1, WORD_LENGTH + 1):
if f"wrong_spot_{i}" in args and getattr(args, f"wrong_spot_{i}"):
total_wrong_spot += len(getattr(args, f"wrong_spot_{i}"))
if args.solved.count("_") < total_wrong_spot:
parser.error(
'More "wrong spot" characters given than there are unsolved '
"characters"
)
return args
def print_words_ordered_by_vowel(word_list):
"""Print a word list ordered by vowel count"""
def print_separator(vowel_count):
print(f"=== {vowel_count} ===")
vowel_count = None
for word in word_list:
if vowel_count is None:
vowel_count = word.get_unique_vowel_count
print_separator(vowel_count)
if word.get_unique_vowel_count < vowel_count:
vowel_count = word.get_unique_vowel_count
print()
print_separator(vowel_count)
print(word)
def get_full_path(file_path):
"""Return a full path to a file
Converts a relative path based on location of this script.
"""
abs_path = file_path
if not abs_path.startswith("/"):
script_dir = Path(__file__).parent
abs_path = str(script_dir / file_path)
return abs_path
def main():
"""Begin execution"""
args = parse_args()
known = vars(args)
dict_path = get_full_path(DICT_FILE)
try:
dictionary = Dictionary(dict_path)
except FileNotFoundError as error:
print(
f"Cannot load '{error.filename}': {error.strerror}\n\n"
"Please install a dictionary or fix its path.",
file=sys.stderr,
)
sys.exit(10)
known = KnownWordle(dictionary, known)
try:
print_words_ordered_by_vowel(
known.get_candidates_ordered_by_vowel_count()
)
sys.stdout.flush()
except BrokenPipeError:
# This handles tools like `head` as per the docs
# https://docs.python.org/3/library/signal.html#note-on-sigpipe
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
sys.exit(1)
if __name__ == "__main__":
main()