-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwordle.py
More file actions
50 lines (40 loc) · 1.54 KB
/
wordle.py
File metadata and controls
50 lines (40 loc) · 1.54 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
import collections
import functools
import math
@functools.cache
def signature(target, guess):
target_chars = list(target)
guess_chars = list(guess)
sig = ['.'] * 5
# Match letters in the correct place first
for i in range(len(guess_chars)):
if target_chars[i] == guess_chars[i]:
target_chars[i] = None
guess_chars[i] = None
sig[i] = 'g'
# Now match letters in the wrong place, biasing left
for i, g in enumerate(guess_chars):
try:
if g:
j = target_chars.index(g)
target_chars[j] = None
sig[i] = 'y'
except ValueError:
pass
return ''.join(sig)
def score(signatures):
counts = collections.Counter(signatures)
return sum([-1 if s == 'ggggg' else n * math.log(n) for s, n in counts.items()])
def _incorrect_place_filter(word, required):
word_letters = collections.Counter(word)
for requirement_letter, requirement_count in required.items():
if word_letters.get(requirement_letter, 0) < requirement_count:
return False
return True
def hard_mode_filter(words, guess, signature):
correct_place_matches = [(i, g) for i, (g, s) in enumerate(zip(guess, signature)) if s == 'g']
for i, g in correct_place_matches:
words = {w for w in words if w[i] == g}
incorrect_place_matches = collections.Counter([g for g, s in zip(guess, signature) if s != '.'])
words = {w for w in words if _incorrect_place_filter(w, incorrect_place_matches)}
return words