Skip to content

Instantly share code, notes, and snippets.

@Kudusch
Last active January 12, 2022 20:18
Show Gist options
  • Save Kudusch/b45cb90b00dcd1f6cab9712ace3e6608 to your computer and use it in GitHub Desktop.
Save Kudusch/b45cb90b00dcd1f6cab9712ace3e6608 to your computer and use it in GitHub Desktop.
library(magrittr)
library(dplyr)
library(stringr)
library(words)
f.solve_wordle <- function(
len=5, # Length of the word to guess
phrase="", # Correct letters in the right position. Use . as wildcard.
correct_chars="", # List of correct letters and the incorrect positions (order does not matter)
incorrect_chars="" # Incorrect letters (order does not matter)
) {
# Filter word-list by length and drop word_length variable
out <- words::words %>%
filter(word_length == len) %>%
select(-word_length)
# Set up variables
incorrect_positions <- correct_chars
if (length(correct_chars) == 0) {
correct_chars <- ""
} else {
correct_chars <- names(correct_chars)
}
# Filter for correct letters in correct places
if (phrase != "") {
out <- filter(out, str_detect(word, pattern = regex(phrase)))
}
# Filter for correct letters
if (is.vector(correct_chars)) {
for (correct_char in correct_chars) {
message(correct_char)
# Filter for correct letters that have to occur multiple times
if (str_detect(phrase, correct_char)) {
out <- filter(out, str_detect(word, pattern = regex(
paste0(
correct_char,
"(?=.*",
correct_char,
")"
)
)))
# Directly filter for correct letters
} else {
out <- filter(out, str_detect(word, pattern = regex(correct_char)))
}
}
}
# For every correct letter, remove words where the letter occurs at the wrong position
if (is.list(incorrect_positions) & length(incorrect_positions)>0) {
for (key in names(incorrect_positions)) {
positions <- str_split(incorrect_positions[key], ",")[[1]]
for (pos in positions) {
filter_pat <- paste0("^.{", as.numeric(pos)-1, "}", key)
out <- filter(out, !str_detect(word, pattern = regex(filter_pat)))
}
}
}
# Remove words that contain incorrect letters
# Check if letter can occur more than once or only once
# If an incorrect letter appears in the list of correct letters
# or the solved phrase, filter out words that have multiple occurrences
# of that letter
if (incorrect_chars != "") {
incorrect_chars <- str_split(incorrect_chars, "")[[1]]
for (incorrect_char in incorrect_chars) {
if (
!incorrect_char %in% correct_chars &&
!str_detect(phrase, incorrect_char)
) {
out <- filter(out, !str_detect(word, pattern = regex(incorrect_char)))
} else {
out <- filter(out, !str_detect(word, pattern = regex(
paste0(
"(",
incorrect_char,
").*\\1"
)
)))
}
}
}
return(out)
}
# In this example there is an "e" at position 3, an "s" at position 5
# The letters "a", "o", "n", and "z" are not in the word
# There is a "u", bit not in positions 1 or 4
f.solve_wordle(
phrase = "..e.s", # Use this to keep track of the green letters
incorrect_chars = "aonz", # Use this to keep track of the gray letters
correct_chars = list( # Use this to keep track of the yellow letters
"u"="1,4"
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment