Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Last active Jan 15, 2022
Embed
What would you like to do?
Given a known Wordle answer, backwards-engineer someone's prior guess
# I tweeted about this:
# https://twitter.com/mattdray/status/1481561924202553346
# Then I blogged about it:
# https://www.rostrum.blog/2022/01/14/wordle/
# remotes::install_github("coolbutuseless/wordle")
eldrow <- function(guess, answer, words = wordle::wordle_dict){
answer <- tolower(answer)
guess <- toupper(guess)
guess_chars <- strsplit(guess, "")[[1]]
answer_chars <- strsplit(answer, "")[[1]]
exact <- ifelse(guess_chars == "G", answer_chars, ".") |>
paste0(collapse = "")
wrong_spot <- gsub(
"\\.", "", ifelse(guess_chars == "Y", answer_chars, ".")
)
exact_chars <- regmatches(exact, gregexpr("\\w", exact))[[1]]
correct_chars_table <- table(
c(exact_chars, wrong_spot[wrong_spot != ""])
)
min_count <- as.vector(correct_chars_table)
names(min_count) <- names(correct_chars_table)
possibles <- wordle::filter_words(
words, exact, wrong_spot, min_count, min_count
)
possibles <- possibles[which(possibles != answer)]
possibles[order(possibles)]
}
guess_2 <- eldrow(guess = "-GGG-", answer = "shirt")
lapply(guess_2, \(x) eldrow("YGG--", x)) |>
lapply(\(x) x[which(x != "shirt")]) |>
setNames(guess_2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment