Skip to content

Instantly share code, notes, and snippets.

@mjnbrn
Created May 30, 2022 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjnbrn/6808334b668a455212e1365f517c4a19 to your computer and use it in GitHub Desktop.
Save mjnbrn/6808334b668a455212e1365f517c4a19 to your computer and use it in GitHub Desktop.
this function helps you solve wordle puzzles.

Three parameters:

  1. yes_letters: a five character string of letters and periods s...d
  2. maybe_letters: an unlimited character string of letters that exist somewhere in the word "rw"
  3. no_letters: an unlimited character string of letters that do NOT exist in the word "zthgf"

We attempt to filter the wordlist in that order, based on parameter availability

If you supply the parameter in that order, names are not needed.

PS C:\> find-wordle "s...d" "wr" "z"
sword
PS C:\>

If you need to supply yes_letters and no_letters but not maybe_letters, then you'll need to supply param names!

PS C:\> find-wordle -yes_letters "s...d" -no_letters "z"
stand
salad
spend
steed
shard
squad
stead
scald
sound
solid
stood
shied
speed
synod
scold
sword
staid
spied
PS C:\>
function find-wordle {
[CmdletBinding()]
param (
[Parameter(Position = 0)]
[string]$yes_letters,
[Parameter(Position = 1)]
[string]$maybe_letters,
[Parameter(Position = 2)]
[string]$no_letters
)
# Import The Word List from the wordle site
$wordle_url = "https://www.nytimes.com/games/wordle"
$wordle = Invoke-RestMethod $wordle_url
$js_url = $wordle_url + "/" + ($wordle -split "`n" | select-string 'src="(main\..*\.js)"></script>').Matches.Groups[1].Value
(((Invoke-RestMethod $js_url) -split ";" | Select-String "var ko") -split "]")[0] -replace "^var ", "$" -replace "\[", "" | Invoke-Expression
# If we have positionals, find them first
if ($yes_letters) {
# Make sure our yes_letters pass regex, provide guidance if not
if ($yes_letters -match "[a-z\.]{5}") {
$ko = $ko -match $yes_letters
}
else { Write-host "yes_letters expects five (5) characters consisting of letters and periods"; break }
}
# Process our maybe_letters after our yes_letters
if ($maybe_letters) {
$m_letters = $maybe_letters -split "" -match "[a-z]"
foreach ($letter in $m_letters) {
$ko = $ko -match $letter
}
}
# Time To Negate
if ($no_letters) {
$nope_letters = $no_letters -split "" -match "[a-z]"
foreach ($letter in $nope_letters) {
$ko = $ko -notmatch $letter
}
}
$ko
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment