-1

我一直在写一个 Wordle 机器人,想看看它是如何处理所有 13,000 个单词的。问题是我通过 for 循环运行它,效率非常低。运行 30 分钟后,它仅达到 5% 左右。我可以一直等,但最终会超过 10 个小时。必须有更有效的方法。我是python的新手,所以任何建议都将不胜感激。

from string import ascii_lowercase
from collections import Counter
import collections
from tqdm import tqdm
import os

#gets the word file and stores it to Constant Words
my_file = open("allowed-guesses.txt", "r")
Cwords = my_file.readlines()


#Used to clear the cmd
def clear(): 
    os.system('cls') #on Windows System


#variables
list = []
removed = []
occurances = {}
topletters = []

#starting guesses
wordtoGuess = "crane"

#resulting values
results = {}
guesses = {}

def main():
    global words
    words = Cwords.copy()
    global realword
    global wordtoGuess
    global total
    total = 0
#Play wordle with every single possible word. It is a lot.
    for xyz in tqdm(words, desc="Loading..."):
        print("")
        print(f"[[[{total}]]]")
        realword = xyz.rstrip()
        count = 0
        words = Cwords.copy()
        list.clear()
        removed.clear()
        topletters.clear()
        while count < 8: #stop after 7+ guesses
            word = wordtoGuess.rstrip()
            pattern = playGame(word).upper().rstrip()
            if pattern == "stop":
                break

            if pattern == "GGGGG":
                results[realword] = count + 1
                total += count + 1
                wordtoGuess = "crane"
                
                clear()
                break
            for y in range(0, len(word.rstrip())):
                pattern = pattern.rstrip()
                if pattern[y] == "G":
                    isletter(word[y], y)
                if pattern[y] == "Y":
                    contains(word[y], y)
                if pattern[y] == "X":
                    nocontains(word[y])

            words = list.copy()
            makeguess()
            count = count + 1
            
    print(f"[[[{total}]]]")

#Find the words that only match the criteria
def contains(letter, place):
    list.clear()
    for x in words:
        if x not in removed:
            if letter in x:
                if letter == x[place]:
                    removed.append(x)
                else:
                    list.append(x)
            else:
                removed.append(x)
def nocontains(letter):
    list.clear()
    for x in words:
        if x not in removed:
            if letter not in x:
                list.append(x)
            else:
                removed.append(x)
def isletter(letter, place):
    list.clear()
    for x in words:
        if x not in removed:
            if letter == x[place]:
                list.append(x)
            else:
                removed.append(x)

#Make guess based on letter popularity of remaming words. Negate duplicates.
def makeguess():
    highest = 0
    letter = 'e'
    occurances.clear()
    for c in ascii_lowercase:
        amount = 0
        for x in words:
            amount = amount + x.count(c)
            if amount > highest:
                letter = c
                highest = amount
        occurances[c] = amount
    k = Counter(occurances)
    high = k.most_common(10)
    for i in high:
        topletters.append(i[0])
    maxscore = 0
    maxword = "hello"
    for z in words:
        score = 0
        if str(topletters[0]) in z:
            score = score + 15
        if str(topletters[1]) in z:
            score = score + 13
        if str(topletters[2]) in z:
            score = score + 12
        if str(topletters[3]) in z:
            score = score + 11
        if str(topletters[4]) in z:
            score = score + 9
        if str(topletters[5]) in z:
            score = score + 8
        if str(topletters[6]) in z:
            score = score + 5
        if str(topletters[7]) in z:
            score = score + 3
        if str(topletters[8]) in z:
            score = score + 2
        if str(topletters[9]) in z:
            score = score + 1
        d = collections.defaultdict(int)
        for c in z:
            d[c] += 1
            if d[c] > 1:
                score = score - 5
        if score > maxscore:
            maxword = z
            maxscore = score
    global wordtoGuess
    wordtoGuess = maxword

#Play the wordle game
def playGame(guess):
    sequence = ""
    guess = guess.rstrip()
    for x in range(0, len(guess)):
        if guess[x] == realword[x]:
            sequence = sequence + "G"
        elif guess[x] in realword:
            sequence = sequence + "Y"
        else:
            sequence = sequence + "X"
    return sequence

#Start the program
main()
4

0 回答 0