Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ====================================================================
- # Enhanced Wordle Solver – Six Guesses Maximum, Improved Suggestions,
- # and Global Letter Frequency Table.
- #
- # This script downloads a list of five–letter words (from the widely used
- # tabatkins/wordle-list), computes a global frequency table for the letters,
- # then enters a game loop.
- #
- # For each guess you enter:
- # 1. Your guess word (must be 5 letters)
- # 2. The feedback mask returned by the game (a string of 5 characters,
- # using E for an exact match, I for a letter in the word but in the
- # wrong position, and W for a wrong letter).
- #
- # Note:
- # - If a letter is flagged I then that letter is definitely not in that
- # position in the answer.
- # - A letter flagged E may still appear elsewhere.
- #
- # The program filters the candidate words based on your feedback and uses
- # letter frequency (computed globally from the full dictionary as well as
- # from the candidate pool) to suggest good next guesses.
- #
- # You have six guesses to solve the puzzle.
- # ====================================================================
- # --- Step 1. Download the word list ---
- $wordListUrl = "https://raw.githubusercontent.com/tabatkins/wordle-list/main/words"
- try {
- Write-Host "Downloading word list..."
- $response = Invoke-WebRequest -Uri $wordListUrl -UseBasicParsing -ErrorAction Stop
- $content = $response.Content
- } catch {
- Write-Error "Error downloading word list: $_"
- exit
- }
- # Process the downloaded list: trim, lowercase, and keep only 5–letter words.
- $wordArray = $content -split "`n" | ForEach-Object { $_.Trim().ToLower() } | Where-Object { $_.Length -eq 5 }
- if (-not $wordArray -or $wordArray.Count -eq 0) {
- Write-Error "Failed to build word list."
- exit
- }
- Write-Host "Loaded" $wordArray.Count "five-letter words."
- # --- Step 2. Build and Display a Global Letter Frequency Table ---
- function Build-LetterFrequency {
- param (
- [Parameter(Mandatory)]
- [array]$words
- )
- $freq = @{}
- foreach ($word in $words) {
- # Only count each letter once per word (to favor words with unique letters).
- ($word.ToCharArray() | Select-Object -Unique) | ForEach-Object {
- if ($freq.ContainsKey($_)) { $freq[$_]++ } else { $freq[$_] = 1 }
- }
- }
- return $freq
- }
- $globalLetterFrequency = Build-LetterFrequency -words $wordArray
- Write-Host "`nGlobal Letter Frequency Table (from all five-letter words):"
- $globalLetterFrequency.GetEnumerator() |
- Sort-Object -Property Value -Descending |
- Format-Table -Property Name, Value -AutoSize
- # --- Step 3. Define the Wordle Feedback Function ---
- function Get-WordleFeedback {
- <#
- .SYNOPSIS
- Simulates the Wordle feedback for a given secret and guess.
- .DESCRIPTION
- For a secret word and a guess, returns a 5–character string where:
- E = the guessed letter is exactly correct (right letter, right spot),
- I = the guessed letter is in the word but in a different spot,
- W = the guessed letter is not in the word (or occurs too many times).
- (A letter flagged I is disallowed from that position in the answer, while an E
- does not preclude that letter appearing again.)
- #>
- param (
- [Parameter(Mandatory)]
- [string]$secret,
- [Parameter(Mandatory)]
- [string]$guess
- )
- $feedback = New-Object char[] 5
- # Build frequency counts for letters in the secret.
- $letterCounts = @{}
- for ($i = 0; $i -lt 5; $i++) {
- $char = $secret[$i]
- if ($letterCounts.ContainsKey($char)) {
- $letterCounts[$char]++
- } else {
- $letterCounts[$char] = 1
- }
- }
- # First pass: mark exact matches as "E".
- for ($i = 0; $i -lt 5; $i++) {
- if ($guess[$i] -eq $secret[$i]) {
- $feedback[$i] = "E"
- $letterCounts[$guess[$i]]--
- }
- }
- # Second pass: for letters not already matched, mark "I" if the letter is present, else "W".
- for ($i = 0; $i -lt 5; $i++) {
- if ($feedback[$i] -ne "E") {
- $char = $guess[$i]
- if ($letterCounts.ContainsKey($char) -and $letterCounts[$char] -gt 0) {
- $feedback[$i] = "I"
- $letterCounts[$char]--
- } else {
- $feedback[$i] = "W"
- }
- }
- }
- return -join $feedback
- }
- # --- Step 4. Functions for Scoring and Suggestion ---
- # Score a word by summing the frequencies of its unique letters (using the given frequency table).
- function Get-GuessScore {
- param (
- [Parameter(Mandatory)]
- [string]$word,
- [Parameter(Mandatory)]
- [hashtable]$frequencyTable
- )
- $score = 0
- $uniqueLetters = $word.ToCharArray() | Select-Object -Unique
- foreach ($letter in $uniqueLetters) {
- if ($frequencyTable.ContainsKey($letter)) {
- $score += $frequencyTable[$letter]
- }
- }
- return $score
- }
- # --- Step 5. Main Game Loop (Six Guesses Maximum) ---
- $candidateWords = $wordArray
- $guessHistory = @()
- for ($attempt = 1; $attempt -le 6; $attempt++) {
- Write-Host "`n---------------------------"
- Write-Host "Attempt $attempt of 6"
- if ($guessHistory.Count) {
- Write-Host "Guess History:"
- $guessHistory | ForEach-Object { Write-Host "$($_.Guess) => $($_.Mask)" }
- }
- Write-Host "Candidates remaining: $($candidateWords.Count)"
- if ($candidateWords.Count -le 50) {
- Write-Host "Candidate words: " ($candidateWords -join ", ")
- }
- if ($candidateWords.Count -eq 1) {
- Write-Host "Only one candidate remains – the answer must be: $($candidateWords[0])"
- break
- }
- # For suggestion, if the candidate pool is large, use the full word list; otherwise use the candidate list.
- $suggestPool = ($candidateWords.Count -gt 100) ? $wordArray : $candidateWords
- $currentFrequency = Build-LetterFrequency -words $suggestPool
- $scoredCandidates = foreach ($w in $candidateWords) {
- [PSCustomObject]@{ Word = $w; Score = (Get-GuessScore -word $w -frequencyTable $currentFrequency) }
- }
- $suggestion = ($scoredCandidates | Sort-Object -Property Score -Descending | Select-Object -First 1).Word
- Write-Host "Suggested guess: $suggestion"
- # Prompt for the guess.
- $guess = Read-Host "Enter your guess word (or type 'exit' to quit)"
- if ($guess.Trim().ToLower() -eq "exit") { break }
- $guess = $guess.Trim().ToLower()
- if ($guess.Length -ne 5) {
- Write-Host "Your guess must be exactly 5 letters. Try again."
- $attempt--
- continue
- }
- # Prompt for the feedback mask.
- $mask = Read-Host "Enter the feedback mask (E=exact, I=wrong spot but in word, W=wrong; e.g., EIWWI)"
- $mask = $mask.Trim().ToUpper()
- if ($mask.Length -ne 5 -or $mask -notmatch '^[EIW]{5}$') {
- Write-Host "Invalid mask. It must be 5 characters (E, I, or W). Try again."
- $attempt--
- continue
- }
- $guessHistory += [PSCustomObject]@{ Guess = $guess; Mask = $mask }
- if ($mask -eq "EEEEE") {
- Write-Host "Congratulations! You solved the Wordle in $attempt attempt(s)!"
- break
- }
- # Filter the candidate words: keep only those words that would produce the same feedback mask.
- $candidateWords = $candidateWords | Where-Object { (Get-WordleFeedback -secret $_ -guess $guess) -eq $mask }
- if ($candidateWords.Count -eq 0) {
- Write-Host "No candidate words remain – please double-check your inputs."
- break
- }
- }
- if ($attempt -gt 6) {
- Write-Host "Maximum of 6 attempts reached – better luck next time!"
- }
- Write-Host "Exiting Wordle Solver. Happy puzzling!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement