Convert solver functions into a solver class.

This is prep for having additional solvers that can be trivially
swapped in and out.
This commit is contained in:
dongar committed 2023-07-08 10:33:54 -05:00
1 parent 0639113205
commit 414380740f
2 files changed
+59 -52

No files matched your search

+55 -50
View File
@@ -3,80 +3,85 @@
import string import string
from collections import defaultdict from collections import defaultdict
import constraints
import dictionary import dictionary
import game
def analyze_dictionary(wordle_dictionary): class SimilaritySolver(object):
# Word Position, Letter: Count @staticmethod
position_matches = [ def _analyze_dictionary(words):
defaultdict(int), # Word Position, Letter: Count
defaultdict(int), position_matches = [
defaultdict(int), defaultdict(int),
defaultdict(int), defaultdict(int),
defaultdict(int), defaultdict(int),
] defaultdict(int),
defaultdict(int),
]
# Letter: Count # Letter: Count
word_matches = defaultdict(int) word_matches = defaultdict(int)
for word in wordle_dictionary: for word in words:
unique_letters = set(word) unique_letters = set(word)
for letter in unique_letters: for letter in unique_letters:
word_matches[letter] += 1 word_matches[letter] += 1
for index in range(len(word)):
letter = word[index]
assert letter in string.ascii_lowercase
position_matches[index][letter] += 1
return position_matches, word_matches
@staticmethod
def _score_word(position_matches, word_matches, word):
result = 0
# Green Score
for index in range(len(word)): for index in range(len(word)):
letter = word[index] letter = word[index]
assert letter in string.ascii_lowercase green_score = position_matches[index][letter]
position_matches[index][letter] += 1 result += 10 * green_score
return position_matches, word_matches # Yellow Score
for letter in set(word):
yellow_score = word_matches[letter]
result += yellow_score
return result
def score_word(position_matches, word_matches, word): def scored_words(self, wordle_dictionary=dictionary.words()):
result = 0 # word: score
scored_words = dict()
# Green Score position_matches, word_matches = self._analyze_dictionary(wordle_dictionary)
for index in range(len(word)):
letter = word[index]
green_score = position_matches[index][letter]
result += 10 * green_score
# Yellow Score for word in wordle_dictionary:
for letter in set(word): scored_words[word] = self._score_word(position_matches, word_matches, word)
yellow_score = word_matches[letter]
result += yellow_score
return result result = list(wordle_dictionary)
result.sort(key=lambda word: scored_words[word], reverse=True)
return [(word, scored_words[word]) for word in result]
def scored_words(wordle_dictionary=dictionary.words()):
# word: score
scored_words = dict()
position_matches, word_matches = analyze_dictionary(wordle_dictionary)
for word in wordle_dictionary:
scored_words[word] = score_word(position_matches, word_matches, word)
result = list(wordle_dictionary)
result.sort(key=lambda word: scored_words[word], reverse=True)
return [(word, scored_words[word]) for word in result]
if __name__ == "__main__": if __name__ == "__main__":
solve = SimilaritySolver()
consts = constraints.Constraints() consts = constraints.Constraints()
consts.update_graded( consts.update_graded(
"cares", "cares",
[ [
solver.LetterResult.INCORRECT, game.LetterResult.INCORRECT,
solver.LetterResult.INCORRECT, game.LetterResult.INCORRECT,
solver.LetterResult.INCORRECT, game.LetterResult.INCORRECT,
solver.LetterResult.INCORRECT, game.LetterResult.INCORRECT,
solver.LetterResult.INCORRECT, game.LetterResult.INCORRECT,
], ],
) )
valid_words = consts.filter_words(dictionary.words()) valid_words = consts.filter_words(dictionary.words())
print("Top: ", solver.scored_words(valid_words)[:5]) print("Top: ", solve.scored_words(valid_words)[:5])
+4 -2
View File
@@ -6,9 +6,11 @@ import solver
class TestSolver(unittest.TestCase): class TestSolver(unittest.TestCase):
def test_scored_words(self): def test_SimilaritySolver(self):
"""Minimal end to end test.""" """Minimal end to end test."""
result = solver.scored_words()
solve = solver.SimilaritySolver()
result = solve.scored_words()
expected = [ expected = [
("cares", 139631), ("cares", 139631),