Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Last active January 15, 2022 00:26
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 matt-dray/fe1f236f21abe7d2e4b2a594d5b207e1 to your computer and use it in GitHub Desktop.
Save matt-dray/fe1f236f21abe7d2e4b2a594d5b207e1 to your computer and use it in GitHub Desktop.
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