Skip to content

Instantly share code, notes, and snippets.

@mbeltagy
Last active January 29, 2022 09:59
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 mbeltagy/51e4672379a71e5d73c789e6106bc296 to your computer and use it in GitHub Desktop.
Save mbeltagy/51e4672379a71e5d73c789e6106bc296 to your computer and use it in GitHub Desktop.
Wordle in Julia gist for use in my blogs
julia> let_in_pos = Dict{Int,Char}();
julia> let_not_in_pos = Dict{Char,Vector{Int}}();
julia> let_not_in_word = Set{Char}();
julia> word_set = copy(words);
julia> word=rand(word_set)
"excel"
julia> update_constraints_by_response!(word, "00020", let_in_pos, let_not_in_pos, let_not_in_word)
julia> word_set_reduction!(word_set, let_in_pos, let_not_in_pos, let_not_in_word)
698-element Vector{String}:
"other"
"water"
"assed"
"osier"
julia> word=rand(word_set)
"buyer"
julia> update_constraints_by_response!(word, "02022", let_in_pos, let_not_in_pos, let_not_in_word)
julia> word_set_reduction!(word_set, let_in_pos, let_not_in_pos, let_not_in_word)
13-element Vector{String}:
"outer"
"super"
"fumer"
"muter"
julia> word=rand(word_set)
"fumer"
julia> update_constraints_by_response!(word, "02022", let_in_pos, let_not_in_pos, let_not_in_word)
julia> word_set_reduction!(word_set, let_in_pos, let_not_in_pos, let_not_in_word)
10-element Vector{String}:
"outer"
"super"
"duper"
"nuder"
julia> word=rand(word_set)
"purer"
julia> update_constraints_by_response!(word, "12022", let_in_pos, let_not_in_pos, let_not_in_word)
julia> word_set_reduction!(word_set, let_in_pos, let_not_in_pos, let_not_in_word)
2-element Vector{String}:
"super"
"duper"
julia> word=rand(word_set)
"super"
let_in_pos = Dict{Int,Char}()
let_not_in_pos = Dict{Char,Vector{Int}}()
let_not_in_word = Set{Char}()
function update_constraints_by_response!(word, response, let_in_pos, let_not_in_pos, let_not_in_word)
for i in eachindex(response)
c = response[i]
if c=='2'
let_in_pos[i]=word[i]
elseif c=='1'
let_not_in_pos[word[i]] = push!(get(let_not_in_pos,word[i],Int[]),i)
else
push!(let_not_in_word,word[i])
end
end
end
julia> words = readlines(download("https://www-cs-faculty.stanford.edu/~knuth/sgb-words.txt"))
5757-element Vector{String}:
"which"
"there"
"biffy"
"pupal"
function word_set_reduction!(word_set, let_in_pos, let_not_in_pos, let_not_in_word)
filter!(w->all(w[r[1]]==r[2] for r in let_in_pos), word_set)
filter!(w->all(occursin(s[1],w) && all(w[p]!=s[1] for p in s[2]) for s in let_not_in_pos), word_set)
filter!(w->all(!occursin(c,w[setdiff(1:5,keys(let_in_pos))]) for c in setdiff(let_not_in_word,keys(let_not_in_pos))), word_set)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment