Last active
January 15, 2022 00:26
-
-
Save matt-dray/fe1f236f21abe7d2e4b2a594d5b207e1 to your computer and use it in GitHub Desktop.
Given a known Wordle answer, backwards-engineer someone's prior guess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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