Skip to content

Instantly share code, notes, and snippets.

@letsbreelhere
Last active January 11, 2022 22:51
Show Gist options
  • Save letsbreelhere/5a05538d4c2e92193f9c455c38160325 to your computer and use it in GitHub Desktop.
Save letsbreelhere/5a05538d4c2e92193f9c455c38160325 to your computer and use it in GitHub Desktop.
Wordle autoplayer
letters =: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
allWords =: > (#~ (5&=@>@(# each))) }: each cutLF fread 'scrabble.txt'
NB. Letters are scored by their frequency's nearness to 50%
NB. and duplicates are counted once, since they provide less information.
bestWord =: monad define
freqs =. (#y) %~ <: #/.~ letters , ,y
scores =. ((1 = freqs) * %10) + 0.5 - | 0.5 - freqs
score =. [: +/ {&scores@(letters&i.)@~.
{. y \: > score"1 y
)
black =: dyad define
x #~ -. +./"1 x e."1 y
)
NB. y is given as a pair: letters vs. positions
green =: dyad define
x #~ */"1 (>{.y) =" 1 (;}.y) {"1 x
)
yellow =: dyad define
indices =: > (<"1 x i."1 >{. y)
forbidden =: ,{<"1] 5,.;}. y
x #~ (*/"1 indices ~: 5) *. -. +./"1 indices ="1 ;}.y
)
filter =: dyad define
colors =: >{.x
blacks =: 'B' = >{.x
yellows =: 'Y' = >{.x
greens =: 'G' = >{.x
word =. >{:x
if. ((#y) < 2) +. */ greens do. y return. end.
if. +./blacks do. y =. y black blacks # word end.
if. +./yellows do. y =. y yellow (word ((yellows#[) ; (yellows#])) i.5) end.
if. +./greens do. y =. y green (word ((greens#[) ; (greens#])) i.5) end.
y
)
round =: monad define
guess =. bestWord y
echo 'Guess ' , guess , ' and input colors (B/Y/G):'
colors =. (1!:1) 1
(1!:2)&2 (colors; guess) filter y
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment