Skip to content

Instantly share code, notes, and snippets.

@mastoj
Created January 11, 2022 21:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mastoj/9ccc61c439dd6e8b372c75d5e29e18b1 to your computer and use it in GitHub Desktop.
Save mastoj/9ccc61c439dd6e8b372c75d5e29e18b1 to your computer and use it in GitHub Desktop.
Naive wordle solver in F#
let candidates = """ACRID AIDER AIRED ARDRI AROID BIDER BIRDS BRAID BREID BRIDE CAIRD CARDI CIDER CRIED DAIRY DARIC DARIS DARZI DEAIR DERIG DIARY DICER DIKER DIMER DINAR DINER DIRAM DIRER DIRGE DIRKE DIRKS DIRLS DIRTS DIRTY DIVER DORIC DORIS DRAIL DRAIN DRIBS DRICE DRIED DRIER DRIES DRIFT DRILL DRILY DRINK DRIPS DRIPT DRIVE DROID DROIL DROIT DRUID DURZI EIDER FIORD FIRED FRIED GIRDS GRIDE GRIDS GRIND HIDER HIRED IDLER INDRI IRADE IRIDS IRKED IZARD JERID JIRDS LAIRD LIARD LIDAR LURID MARID MIRED MUDIR MURID NADIR NIDOR PADRI PARDI PRIDE PRIED RABID RADII RADIO RADIX RAIDS RAIRD RANID RAPID REBID REDIA REDID REDIP REIRD RESID RICED RIDER RIDES RIDGE RIDGY RIGID RILED RIMED RINDS RINDY RIPED RIVED RORID RUDIE SIDER SIRED THIRD THRID TIRED TRIAD TRIDE TRIED TRILD UNRID URSID VIRED VIRID WEIRD WIDER WIRED WRIED YAIRD YIRDS YRIVD"""
let wordList = candidates.Split(" ") |> List.ofArray
let fixedLetters = [(2, 'I')] |> Map.ofList
let excludedCharacters = "WE"
let includeLetters = [('R', [3]);('D',[4])] |> List.map (fun (x,y) -> x,(y |> Set.ofList))
let filterCandidates (wordList: string list) (fixedLetters: Map<int, char>) (excludedCharacters: string) (includeLetters: (char*(Set<int>)) list) =
let excludedCharactersArr = excludedCharacters.ToCharArray()
let result =
wordList
|> List.filter (fun w ->
let chars = w.ToCharArray()
let letterComparison =
[ for i in 0 .. 4 do
let currentChar = chars[i]
if excludedCharactersArr |> Array.contains currentChar then yield false
else if fixedLetters |> Map.containsKey i && fixedLetters[i] <> currentChar then yield false
else yield true
] |> List.contains false |> not
let wordIndicesLookup = chars |> Array.mapi (fun x c -> c,x) |> Array.groupBy fst |> Map.ofArray |> Map.map (fun _ grp -> grp |> Array.map snd |> Set.ofArray)
let includeComparison =
includeLetters
|> List.map (fun (letter, indices) ->
let letterIndices = wordIndicesLookup |> Map.tryFind letter |> Option.defaultValue Set.empty
let diff = Set.difference letterIndices indices
diff |> Set.isEmpty |> not
) |> List.contains false |> not
letterComparison && includeComparison
)
result
filterCandidates wordList fixedLetters excludedCharacters includeLetters
|> printfn "==> Filtered candidates: %A"
@mastoj
Copy link
Author

mastoj commented Jan 11, 2022

Start with making one guess, I usually start with "weird" for some reason. Based on the result there you can go over to https://www.bestwordlist.com/ and find possible 5 letter words. As you make more and more guesses you can fill in the variables fixedLetters(those that have a fixed position), excludeLetters and includeLetters which also has a list of position they should NOT be in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment