diff --git a/src/literate_wordle/guess.py b/src/literate_wordle/guess.py index 48213dc..6d8a994 100644 --- a/src/literate_wordle/guess.py +++ b/src/literate_wordle/guess.py @@ -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)])