Skip to content

Commit

Permalink
Fix bug #1 via first pass for perfects
Browse files Browse the repository at this point in the history
Get rid of the perfect scores in a first pass, to avoid bug
  • Loading branch information
Jb DOYON committed May 21, 2022
1 parent c68ce8d commit 6f11c4c
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/literate_wordle/guess.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,28 @@ def score_guess(guess: str, answer: str) -> str:
"""Score an individual guess with Counter"""
# Counter("abbey") = Counter({'b': 2, 'a': 1, 'e': 1, 'y': 1})
answer_chars = Counter(answer)
response = ""
for guess_char, answer_char in zip(guess, answer):
response = {}
perfect_scores = [answer_char == guess_char for guess_char, answer_char in zip(guess, answer)]
for char_index, is_score_perfect in enumerate(perfect_scores):
perfect_char = guess[char_index]
if is_score_perfect:
response[char_index] = CharacterScore.OK
answer_chars[perfect_char] -= 1
if answer_chars[perfect_char] == 0:
del answer_chars[perfect_char]
# First pass complete for perfect scores, now the non-perfect
for char_num, (guess_char, answer_char) in enumerate(zip(guess, answer)):
if char_num in response:
continue # No point scoring this one, we got it already as perfect
if guess_char not in answer_chars:
response += CharacterScore.NO
response[char_num] = CharacterScore.NO
continue # Early exit for this character, skip to next
# From here on, we MUST have a char in common, regardless of place
if answer_char == guess_char:
response += CharacterScore.OK
elif answer_chars[guess_char] > 0:
response += CharacterScore.WRONG_PLACE
response[char_num] = CharacterScore.WRONG_PLACE
# Either way, reduce occurence counter since we "used" this occurence
answer_chars[guess_char] -= 1
# No more hits = pretend character isn't even seen (remove from dict)
if answer_chars[guess_char] == 0:
del answer_chars[guess_char]
return response
return "".join([response[i] for i in range(5)])

0 comments on commit 6f11c4c

Please sign in to comment.