GillesVandewiele / Wordle-Bot

An entropy-based strategy to wordle
MIT License
27 stars 11 forks source link

`calculate_pattern` function does not take into account duplicate letters #2

Closed MarkCBell closed 2 years ago

MarkCBell commented 2 years ago

For example, calculate_pattern('meets', 'weary') currently returns (0, 2, 1, 0, 0), however the correct pattern should be (0, 2, 0, 0, 0) since there is not a second e in the target word. One way to do this correctly is:

from collections import Counter

def calculate_pattern(guess, true):
    '''Yield the pattern that Wordle would return if you guessed `guess` and the true word is `true`'''
    yellows = Counter(true)
    for x, y in zip(guess, true):
        if x == y:
            yield 2
        elif yellows[x] > 0:
            yield 1
        else:
            yield 0
        yellows[x] -= 1

you just need to then call this as tuple(calculate_pattern(word, word2)).

GillesVandewiele commented 2 years ago

Wow, thank you so much for spotting this. I implemented your fix (and credited you in the code)

gbotev1 commented 2 years ago

I think you need to do two passes in order to also catch corner cases like calculate_pattern('rower', 'goner'), which currently returns (1, 2, 0, 2, 2) instead of (0, 2, 0, 2, 2).