Refactor constraints into it's own file.

This commit is contained in:
dongar committed 2023-05-23 12:29:31 -05:00
1 parent 7cf685b507
commit 36c26dcf9f
4 files changed
+153 -150

No files matched your search

+4 -67
View File
@@ -2,11 +2,11 @@
import random
import string
from enum import Enum
from collections import defaultdict
import dictionary
import constraints
class LetterResult(Enum):
@@ -137,72 +137,9 @@ def scored_words(wordle_dictionary=dictionary.words()):
return [(word, scored_words[word]) for word in result]
class Constraints:
def __init__(self):
# Array of known letters in result with None for unknown.
self.correct = [None] * dictionary.WORDLE_LENGTH
# Array with per-letter sets of known bad letters.
self.incorrect = [set() for _ in range(dictionary.WORDLE_LENGTH)]
# Dict of letter -> int with minimum required count of letter in word.
self.count = defaultdict(int)
# If letter is present in set, value in min is both min and max.
self.exact = set()
# List of LetterResult values.
def update_graded(self, guess, grade):
guess = guess.lower()
new_count = defaultdict(int)
for i, letter in enumerate(guess):
match grade[i]:
case LetterResult.CORRECT:
self.correct[i] = letter
new_count[letter] += 1
case LetterResult.MOVE:
self.incorrect[i].add(letter)
new_count[letter] += 1
case LetterResult.INCORRECT:
self.incorrect[i].add(letter)
self.exact.add(letter)
new_count[letter] += 0 # Ensure letter is in dict
for letter, c in new_count.items():
self.count[letter] = max(c, self.count[letter])
def matches(self, guess):
counts = defaultdict(int)
# All known correct letters in place.
for i, letter in enumerate(guess):
# If we have a known letter, and don't match...
if self.correct[i] and self.correct[i] != letter:
return False
# If we have a letter that's known to be incorrect in the current location...
if letter in self.incorrect[i]:
return False
counts[letter] += 1
for letter, c in self.count.items():
if letter in self.exact:
if counts[letter] != c:
return False
else:
if counts[letter] < c:
return False
return True
def filter_dictionary(constraints, wordle_dictionary=dictionary.words()):
return set(filter(lambda w: constraints.matches(w), wordle_dictionary))
if __name__ == "__main__":
constraints = Constraints()
constraints.update_graded(
consts = constraints.Constraints()
consts.update_graded(
"cares",
[
LetterResult.INCORRECT,
@@ -213,6 +150,6 @@ if __name__ == "__main__":
],
)
valid_words = filter_dictionary(constraints)
valid_words = consts.filter_words(dictionary.words())
print("Top: ", scored_words(valid_words)[:5])