mikhail_dvorkin icon

Wordle strategy

mikhail_dvorkin | PRO | 05/24/22 09:51:44 AM UTC | 0 ⭐ | 1840 👁️ | Never ⏰ | []
Python |

1.76 KB

|

None

|

0 👍

/

0 👎

import math
 
def guess(attempt, secret):
    n = len(secret)
    assert n == len(attempt), (attempt, secret, n)
    result = ['-'] * n
    unused = list(secret)
    for i in range(n):
        if attempt[i] == secret[i]:
            result[i] = '+'
            unused[i] = None
    for i in range(n):
        if result[i] == '-' and attempt[i] in unused:
            result[i] = '?'
            unused[unused.index(attempt[i])] = None
    return ''.join(result)
 
def best_move(possible_words):
    if len(possible_words) == 2315:
        return "raise"
    if len(possible_words) == 3483:
        return "норка"
    if len(possible_words) == 8363:
        return "12953"
    best_score = 1e100
    best_move = None
    for move in words:
        score = 0.0 # чем меньше, тем лучше
        if move in possible_words:
            score -= 1.0
        groups = {}
        for secret in possible_words:
            result = guess(move, secret)
            groups[result] = groups.get(result, 0) + 1
        for group_size in groups.values():
            score += group_size * math.log2(group_size)
        if score < best_score:
            best_score = score
            best_move = move
    return best_move
 
def is_prime(p):
    for x in range(2, round(p ** 0.5) + 1):
        if p % x == 0:
            return False
    return p > 1
 
def primes(n=5):
    return [str(p) for p in range(10**(n - 1), 10**n) if is_prime(p)]
 
 
# https://www.nytimes.com/games/wordle/
# https://wordle-igra.ru/
# https://wordle.belousov.one/
# https://converged.yt/primel/
words = open("ru_nounlist.txt", encoding="utf-8").read().split()
words = [word for word in words if len(word) == 5]
# words = primes()
possible_words = words
while len(possible_words) > 1:
    move = best_move(possible_words)
    print(move)
    result = input()
    possible_words = [word for word in possible_words if result == guess(move, word)]
    print("Info:", len(possible_words), possible_words)
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎