#๐Ÿ”’ 6 letter wordle algorithm

42 messages ยท Page 1 of 1 (latest)

pine windBOT
#

@opaque trail

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

opaque trail
#

need help to know how to improve a code by reducing the avg number of guesses in takes to guess the correct word, below is what i am ment to modify its at 5 right now but ideally i need it 3-3.5, i dont know hopw to improve

#

import WordSolver
from collections import Counter
import random

class SmartSolver(WordSolver.Solver):
def newGame(self):
self.wordsRemaining = list(self.wordList)

def guess(self):
    if self.guesses:  # Filter based on previous feedback
        self.wordsRemaining = [w for w in self.wordsRemaining if self.isWordValid(w)]
    return self.choose_best_word()

def isWordValid(self, word):
    # Validate the word against all previous guesses
    for guess in self.guesses:
        for i, letter in enumerate(word):
            response = guess['response'][i]
            if (response == '+' and letter != guess['word'][i]) or \
               (response == '*' and (letter == guess['word'][i] or letter not in word)) or \
               (response == '-' and letter == guess['word'][i]):
                return False
    return True

def choose_best_word(self):
    # Generate frequency count for each letter at each position
    pos_freq = [Counter(w[i] for w in self.wordsRemaining) for i in range(len(self.wordsRemaining[0]))]

    # Score words based on how common their letters are at each position
    def score_word(word):
        return sum(pos_freq[i][letter] for i, letter in enumerate(word))

    # Return the word with the highest score
    return max(self.wordsRemaining, key=score_word, default=random.choice(self.wordsRemaining))

s = SmartSolver()
Naverage = s.testSolver()
print(f"Average number of guesses = {Naverage}")

pine windBOT
#

Hey @opaque trail!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
opaque trail
#

along side this there is also a word list of 4000 sixletter words

#

import random

class Solver:
def init(self):
self.guesses = [] # A list of all guesses made so far with results
with open('wordList.txt') as f:
self.wordList = tuple(word.rstrip() for word in f) # The complete list of possible words.

def play(self):                                             # Plays a single game - this method should NOT be overridden
    self.guesses = []                                       # Reset guesses list to nothing
    self.newGame()                                          # Any additional initialisation can go in the newGame() method
    isSolved = False
    numGuesses = 0
    answer = random.choice(self.wordList)                   # Choose a word at random from the list
    while not isSolved and numGuesses < 10000:              # Loop until solved or the 10000 guess limit is reached
        g = self.guess()                                    # The guess() method will need overriding in inherited SmartSolvers
        numGuesses = numGuesses + 1
        response = ['+' if x == y else '-' for x,y in zip(g, answer)]           # Check each letter for '+' result
        lettersRemaining = [a for a,r in zip(answer,response) if r == '-']      # All letters not already graded '+'
        for i,c in enumerate(g):
            if response[i] == '-' and c in lettersRemaining:                    # If a letter is in the word but elsewhere...
                response[i] = '*'                                               # ...response = '*'
                j = lettersRemaining.index(c)
                lettersRemaining = lettersRemaining[:j]+lettersRemaining[(j+1):]    # Once found, remove from list in case of duplicates
        self.guesses.append({'word':g, 'response':''.join(response)})
        isSolved = (g == answer)
    return numGuesses
#

def testSolver(self, numGames=100):
t = 0
for n in range(numGames):
t = t + self.play()
return t / numGames

def newGame(self):
    pass                # Override if any initialisation is needed at the start of a game.

def guess(self):
    return "aaaaaa"     # SmartSolvers will override this to give better guesses.
plucky vapor
#

!code

pine windBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

plucky vapor
#

I don't think your isWordValid is specific enough. For example, if I guess a word with 2 o's, and one is returned as right but in the wrong place and the other isn't, I know the word has exactly 1 o.

#

I would break out the response calculating into its own function and use that same response function for both play and isvalid and have your isvalid just check the response for each possible answer given your guess to make sure it gives the same response that you have.

opaque trail
#

sorry to do this

#

but i am quite new to coding

plucky vapor
#

Next, instead of your frequency calculation, I would do an entropy calculation. The best words aren't the ones that are guessing really common remaining letters, they are the ones that divide the results into lots of little buckets based on the response.

opaque trail
plucky vapor
#

I would take the chunk of code that calculates the response and put it into a separate function, that way you can use it in multiple spots in the code

opaque trail
#

ah

#

ok

#

do u think these 2 changes will be enough to bring the avg guesses below 4?

plucky vapor
#

Probably. Using entropy is a bit of a shortcut to estimate how many guesses you'll have left for a bucket of a given size. There are methods that aren't just using estimates, but I would think using entropy will get you close enough to the optimal answer that if you're being told you need to get the performance below 4, then entropy will probably do it.

opaque trail
#

ok ok

#

i will try that

#

an let u know

#

thansk man

#

๐Ÿ‘

#

i have my friends code

#

thats entropy based

#

but like

#

i have no clue what to do wit it

#

it jus gives me entropy based on each word in the shell

#

it prolly is ai tho cl

plucky vapor
#

Okay, than you probably want the word that gives you the highest or lowest entropy, depending on how they are using it

opaque trail
#

so what do i do with it further

#

i cant paste it

#

unless i do it in alot of sections

#

should i paste it?

plucky vapor
#

Here is a really good video that explains why entropy works: https://www.youtube.com/watch?v=v68zYyaEmEA . There is a really intuitive explanation for what, at first, seems like a complicated formula.

An excuse to teach a lesson on information theory and entropy.
These lessons are funded by viewers: https://www.patreon.com/3blue1brown
Special thanks to these supporters: https://3b1b.co/lessons/wordle#thanks
An equally valuable form of support is to simply share the videos.

Contents:
0:00 - What is Wordle?
2:43 - Initial ideas
8:04 - Informat...

โ–ถ Play video
opaque trail
#

ok ok

#

thanks boo

pine windBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.