-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.py
executable file
·612 lines (459 loc) · 18.8 KB
/
wordle.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
import numpy as np
import string
from numba import jit
from abc import ABC, abstractmethod
import array
import time
alphabet = string.ascii_lowercase
def get_match_code_game(guess, answer):
result = array.array("u", ["0", "0", "0", "0", "0"])
answer_char_count = np.zeros((len(alphabet)), dtype=int)
for letter in answer:
answer_char_count[ord(letter) - 97] += 1
# Find exact matches
for idx, letter in enumerate(guess):
if letter == answer[idx]:
result[idx] = "2"
alphabet_idx = ord(letter) - 97
answer_char_count[alphabet_idx] -= 1
# Find inexact matches
for idx, letter in enumerate(guess):
alphabet_idx = ord(letter) - 97
if answer_char_count[alphabet_idx] > 0 and letter != answer[idx]:
result[idx] = "1"
answer_char_count[alphabet_idx] -= 1
return result.tounicode()
@jit(nopython=True, nogil=True, cache=True)
def get_match_code_int(guess_numba, answer_numba, answer_char_counts):
result = 0
answer_char_counts = answer_char_counts.copy()
# Find exact matches
for letter_idx in range(5):
alphabet_idx = guess_numba[letter_idx]
if guess_numba[letter_idx] == answer_numba[letter_idx]:
result += 2 << letter_idx * 2
answer_char_counts[alphabet_idx] -= 1
# Find inexact matches
for letter_idx in range(5):
alphabet_idx = guess_numba[letter_idx]
# If letter in answer
if (
answer_char_counts[alphabet_idx] > 0
and guess_numba[letter_idx] != answer_numba[letter_idx]
):
result += 1 << letter_idx * 2
answer_char_counts[alphabet_idx] -= 1
return result
@jit(nopython=True, nogil=True, cache=True)
def get_bin_counts(guesses, answers, answers_char_counts):
n_codes = 1024 # 2 bits for 5 characters gives max 1024
counts = np.zeros((n_codes, guesses.shape[0]), dtype=np.intc)
for guess_idx in range(guesses.shape[0]):
guess_array = guesses[guess_idx, :]
for answer_idx in range(answers.shape[0]):
result = get_match_code_int(
guess_array,
answers[answer_idx, :],
answers_char_counts[answer_idx, :],
)
counts[result, guess_idx] += 1
return counts
@jit(nopython=True, nogil=True, cache=True)
def get_bin_counts_inline(guesses, answers, answers_char_counts):
n_codes = 1024 # 2 bits for 5 characters gives max 1024
counts = np.zeros((n_codes, guesses.shape[0]), dtype=np.intc)
for guess_idx in range(guesses.shape[0]):
guess_numba = guesses[guess_idx, :]
answer_char_counts_g = answers_char_counts.copy()
for answer_idx in range(answers.shape[0]):
result = 0
answer_numba = answers[answer_idx, :]
answer_char_counts = answer_char_counts_g[answer_idx, :]
# Find exact matches
for letter_idx in range(5):
alphabet_idx = guess_numba[letter_idx]
if guess_numba[letter_idx] == answer_numba[letter_idx]:
result += 2 << letter_idx * 2
answer_char_counts[alphabet_idx] -= 1
# Find inexact matches
for letter_idx in range(5):
alphabet_idx = guess_numba[letter_idx]
# If letter in answer
if (
answer_char_counts[alphabet_idx] > 0
and guess_numba[letter_idx] != answer_numba[letter_idx]
):
result += 1 << letter_idx * 2
answer_char_counts[alphabet_idx] -= 1
counts[result, guess_idx] += 1
return counts
# Faster than scipy implementation
def entropy(counts):
probabilities = counts / np.sum(counts, axis=0)
return -np.sum(
probabilities
* np.log10(
probabilities, out=np.zeros_like(probabilities), where=probabilities != 0
),
axis=0,
)
def information_gain(bin_counts):
full_entropy = -np.log(1 / bin_counts.shape[1])
conditional_entropy = (
bin_counts
/ bin_counts.sum(axis=0)
* -np.log(
np.divide(
1, bin_counts, out=np.zeros(bin_counts.shape), where=bin_counts != 0
),
out=np.zeros(bin_counts.shape),
where=bin_counts != 0,
)
)
return full_entropy - np.nansum(conditional_entropy, axis=0)
# Using Dvoretzky-Kiefer-Wolfowitz inequality
def min_samples(eps, alpha):
return (1 / (2 * eps**2)) * np.log(2 / alpha)
@jit(nopython=True, nogil=True, cache=True)
def get_bin_counts_approximate(guesses, answers, answers_char_counts, sample_size):
n_codes = 1024 # 2 bits for 5 characters gives max 1024
counts = np.zeros((n_codes, guesses.shape[0]), dtype=np.intc)
sample_size = (np.minimum(len(answers), sample_size),)
for guess_idx in range(guesses.shape[0]):
guess_array = guesses[guess_idx, :]
# This could be moved out of the loop by adding another dimension
sub_answer_idxs = np.random.choice(len(answers), size=sample_size, replace=True)
for sub_answer_idx in range(sub_answer_idxs.shape[0]):
answer_idx = sub_answer_idxs[sub_answer_idx]
result = get_match_code_int(
guess_array,
answers[answer_idx, :],
answers_char_counts[answer_idx, :],
)
counts[result, guess_idx] += 1
return counts
@jit(nopython=True, nogil=True, cache=True)
def get_bin_table(guesses, answers, answers_char_counts):
bin_table = np.zeros((guesses.shape[0], answers.shape[0]), dtype=np.uint)
for guess_idx in range(guesses.shape[0]):
guess_array = guesses[guess_idx, :]
for answer_idx in range(answers.shape[0]):
bin_table[guess_idx, answer_idx] = get_match_code_int(
guess_array,
answers[answer_idx, :],
answers_char_counts[answer_idx, :],
)
return bin_table.T
@jit(nopython=True, nogil=True, cache=True)
def get_bin_table_inline(guesses, answers, answers_char_counts):
bin_table = np.zeros((guesses.shape[0], answers.shape[0]), dtype=np.uint)
for guess_idx in range(guesses.shape[0]):
guess_numba = guesses[guess_idx, :]
answer_char_counts_g = answers_char_counts.copy()
for answer_idx in range(answers.shape[0]):
result = 0
answer_numba = answers[answer_idx, :]
answer_char_counts = answer_char_counts_g[answer_idx, :]
# Find exact matches
for letter_idx in range(5):
alphabet_idx = guess_numba[letter_idx]
if guess_numba[letter_idx] == answer_numba[letter_idx]:
result += 2 << letter_idx * 2
answer_char_counts[alphabet_idx] -= 1
# Find inexact matches
for letter_idx in range(5):
alphabet_idx = guess_numba[letter_idx]
# If letter in answer
if (
answer_char_counts[alphabet_idx] > 0
and guess_numba[letter_idx] != answer_numba[letter_idx]
):
result += 1 << letter_idx * 2
answer_char_counts[alphabet_idx] -= 1
bin_table[guess_idx, answer_idx] = result
return bin_table.T
@jit(nopython=True, nogil=True, cache=True)
def bin_table_to_counts(bin_table, guess_mask, answer_mask):
n_codes = 1024 # 2 bits for 5 characters gives max 1024
# Returns non zero index for each axis, get first axis
guess_idxs = np.nonzero(guess_mask)[0]
answer_idxs = np.nonzero(answer_mask)[0]
counts = np.zeros((n_codes, len(guess_idxs)), dtype=np.intc)
for guess_idx in range(len(guess_idxs)):
for answer_idx in answer_idxs:
counts[bin_table[answer_idx, guess_idxs[guess_idx]], guess_idx] += 1
return counts
@jit(nopython=True, nogil=True, cache=True)
def mask_candidates(match_int, guess_numba, words_numba, word_char_counts, mask):
match_codes = np.full((mask.shape[0],), 0)
for idx in range(mask.shape[0]):
if mask[idx]:
match_codes[idx] = get_match_code_int(
guess_numba, words_numba[idx, :], word_char_counts[idx, :]
)
return mask & (match_codes == match_int)
def get_numeric_representations(wordlist):
words_numba = np.zeros((len(wordlist), 5), dtype=np.intc)
words_char_counts = np.zeros((len(wordlist), len(alphabet)), dtype=np.intc)
for w, word in enumerate(wordlist):
for letter_idx, letter in enumerate(word):
words_numba[w, letter_idx] = ord(letter) - 97
words_char_counts[w, ord(letter) - 97] += 1
return words_numba, words_char_counts
class Game:
def __init__(self, word=None, answers=None, max_plays=6, verbose=False):
if word:
self.word = word
elif answers:
self.word = np.random.choice(answers)
else:
raise ValueError("Must provide word or answers.")
self.max_plays = max_plays
self.guess_list = []
self.verbose = verbose
def play(self, guess):
if len(self.guess_list) < self.max_plays - 1:
if guess != self.word:
self.guess_list.append(guess)
code = get_match_code_game(guess, self.word)
if self.verbose:
print(guess, code)
return code
return None
class Agent(ABC):
@staticmethod
@abstractmethod
def order_guesses(bin_counts):
pass
@abstractmethod
def __init__(self, answers, guesses, mode="standard", first_guess=None):
pass
@abstractmethod
def play(self, game):
pass
class NumbaAgent(Agent):
# @profile
def __init__(
self,
answers,
guesses,
mode="standard",
first_guess=None,
guess_data=None,
answer_data=None,
bin_table=None,
):
self.answers = answers
self.guesses = guesses
self.mode = mode
if type(first_guess) == dict:
self.first_guess = first_guess[mode]
else:
self.first_guess = first_guess
if not guess_data:
self.guesses_numba, self.guesses_char_counts = get_numeric_representations(
self.guesses
)
else:
self.guesses_numba, self.guesses_char_counts = guess_data
if not answer_data:
self.answers_numba, self.answers_char_counts = get_numeric_representations(
self.answers
)
else:
self.answers_numba, self.answers_char_counts = answer_data
self.bin_table = bin_table
# @profile
def play(self, game):
guess_history = []
code_history = []
guess_total_mask = np.ones(len(self.guesses)).astype(bool)
answer_total_mask = np.ones(len(self.answers)).astype(bool)
if self.first_guess:
guess_history.append(self.first_guess)
state = game.play(self.first_guess)
code_history.append(state)
if not state:
return guess_history[-1], len(guess_history)
while True:
if len(guess_history) > 0:
guess_idx = self.guesses.index(guess_history[-1])
# Translate match_code into integer
match_int = 0
for idx, c in enumerate(code_history[-1]):
match_int += int(c) << idx * 2
# Filter guesses and answers based on previous play
if self.mode == "standard":
# Exclude previously guessed words
guess_total_mask[guess_idx] = False
else:
guess_total_mask = mask_candidates(
match_int,
self.guesses_numba[guess_idx, :],
self.guesses_numba,
self.guesses_char_counts,
guess_total_mask,
)
answer_total_mask = mask_candidates(
match_int,
self.guesses_numba[guess_idx, :],
self.answers_numba,
self.answers_char_counts,
answer_total_mask,
)
# Determine best guess
if np.sum(answer_total_mask) > 1:
if type(self.bin_table) != type(None):
bin_counts = bin_table_to_counts(
self.bin_table, guess_total_mask, answer_total_mask
)
else:
bin_counts = get_bin_counts_inline(
self.guesses_numba[guess_total_mask, :].squeeze(),
self.answers_numba[answer_total_mask, :].squeeze(),
self.answers_char_counts[answer_total_mask, :].squeeze(),
)
sort_idx = self.order_guesses(bin_counts)
# Select best word
remaining_guesses = np.array(self.guesses)[guess_total_mask]
remaining_guesses_sorted = remaining_guesses[sort_idx]
guess = remaining_guesses_sorted[0]
else:
guess = np.array(self.answers)[answer_total_mask][0]
guess_history.append(guess)
# Play
state = game.play(guess)
code_history.append(state)
if not state:
break
return guess_history[-1], len(guess_history)
class MaxInfoAgent(NumbaAgent):
def __init__(
self,
answers,
guesses,
mode="standard",
first_guess={"standard": "reast", "hard": "reast"},
guess_data=None,
answer_data=None,
bin_table=None,
):
super().__init__(
answers, guesses, mode, first_guess, guess_data, answer_data, bin_table
)
@staticmethod
def order_guesses(bin_counts):
# Compute Entropy
guesses_entropy = entropy(bin_counts)
# Sort entropy
sort_idx = np.flip(np.argsort(guesses_entropy))
return sort_idx
class MaxSplitsAgent(NumbaAgent):
def __init__(
self,
answers,
guesses,
mode="standard",
first_guess={"standard": "trace", "hard": "salet"},
guess_data=None,
answer_data=None,
bin_table=None,
):
super().__init__(
answers, guesses, mode, first_guess, guess_data, answer_data, bin_table
)
@staticmethod
def order_guesses(bin_counts):
# Compute Number of Splits
guesses_nsplits = np.count_nonzero(bin_counts, axis=0)
# Sort splits
sort_idx = np.flip(np.argsort(guesses_nsplits))
return sort_idx
class MaxPruneAgent(NumbaAgent):
def __init__(
self,
answers,
guesses,
mode="standard",
first_guess={"standard": "laten", "hard": "leant"},
guess_data=None,
answer_data=None,
bin_table=None,
):
super().__init__(
answers, guesses, mode, first_guess, guess_data, answer_data, bin_table
)
@staticmethod
def order_guesses(bin_counts):
total_unmatched = bin_counts[0, :]
sort_idx = np.argsort(total_unmatched)
return sort_idx
class Solver(ABC):
@abstractmethod
def __init__(self, answers, guesses, mode="standard"):
pass
@abstractmethod
def step(self, code=None, guess=None):
pass
class NumbaSolver(Solver):
def __init__(self, answers, guesses, mode="standard"):
self.answers = answers
self.guesses = guesses
self.guesses_numba, self.guesses_char_counts = get_numeric_representations(
self.guesses
)
self.answers_numba, self.answers_char_counts = get_numeric_representations(
self.answers
)
self.guess_history = []
self.code_history = []
self.guess_total_mask = np.ones(len(self.guesses)).astype(bool)
self.answer_total_mask = np.ones(len(self.answers)).astype(bool)
self.mode = mode
class MaxInfoSolver(NumbaSolver):
def step(self, code=None, guess=None):
if code:
self.guess_history.append(guess)
self.code_history.append(code)
guess_idx = self.guesses.index(self.guess_history[-1])
# Translate match_code into integer
match_int = 0
for idx, c in enumerate(self.code_history[-1]):
match_int += int(c) << idx * 2
# Exclude previously guessed words
if self.mode == "standard":
# Exclude previously guessed words
self.guess_total_mask[guess_idx] = False
else:
self.guess_total_mask = mask_candidates(
match_int,
self.guesses_numba[guess_idx, :],
self.guesses_numba,
self.guesses_char_counts,
self.guess_total_mask,
)
self.answer_total_mask = mask_candidates(
match_int,
self.guesses_numba[guess_idx, :],
self.answers_numba,
self.answers_char_counts,
self.answer_total_mask,
)
if np.sum(self.answer_total_mask) > 1:
# For whatever reason, indexing like this adds a dimension so we
# squeeze the dimensions
bin_counts = get_bin_counts_inline(
self.guesses_numba[self.guess_total_mask, :].squeeze(),
self.answers_numba[self.answer_total_mask, :].squeeze(),
self.answers_char_counts[self.answer_total_mask, :].squeeze(),
)
# Compute Entropy
guesses_entropy = entropy(bin_counts)
# Sort by entropy
sort_idx = np.argsort(guesses_entropy)
remaining_guesses = np.array(self.guesses)[self.guess_total_mask]
remaining_guesses_sorted = remaining_guesses[sort_idx]
guess = remaining_guesses_sorted[-1]
else:
guess = np.array(self.answers)[self.answer_total_mask][0]
return guess, np.array(self.answers)[self.answer_total_mask]