-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.R
191 lines (153 loc) · 6.14 KB
/
functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# functions to search dictionary
# load packages
list.of.packages <- c("stringr", "tidyverse", "words")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
library(stringr)
library(tidyverse)
library(words)
# import scrabble dictionary
words5 <- words::words
# filter 5 letter words
words5 <- words5%>%
filter(word_length == 5)
# find all combinations of 5 words that search 25 unique letters
# note: no "vibex"
# form one-hot matrix of letters
onehot_words5 <- matrix(0,
nrow = nrow(words5),
ncol = length(letters))
for (i in 1:length(words5$word)){
word_i <- words5$word[i]
onehot_words5[i,] <- letters %in% unlist(str_extract_all(word_i, ""))
}
unique_letters <- rowSums(onehot_words5)
words5 <- cbind(words5,onehot_words5, unique_letters)
names(words5)[3:28] <- letters
# 5 letter word solver
five_solver <- function(word_str, match_n = 5, not_letters = "", words_full_df = words5){
# check if word in list
if (!word_str %in% words_full_df$word){
warning("Word not in word list")
}
letters_used <- letters %in% unlist(str_extract_all(word_str, ""))
letters_not_used <- letters %in% unlist(str_extract_all(not_letters, ""))
overlap_matrix <- matrix(unlist(rep(letters_used, nrow(words_full_df))),
nrow = nrow(words_full_df),
byrow = TRUE) &
as.matrix(words_full_df[,letters])
# adjust match_n for duplicates
match_n <- min(match_n, sum(letters_used))
overlap_words <- apply(overlap_matrix, 1, sum) >= match_n
remaining_df <- words_full_df[overlap_words,]
if (nrow(remaining_df) == 0){
stop("no words found")
}
dont_match_matrix <- matrix(
unlist(rep(letters_not_used, nrow(remaining_df))),
nrow = nrow(remaining_df),
byrow = TRUE) &
as.matrix(remaining_df[,letters])
dont_match_words <- apply(dont_match_matrix, 1, any)
remaining_df <- remaining_df[!dont_match_words,]
word_list <- remaining_df$word
return(word_list)
}
wordle_help <- function(word_str, match_pattern = "00000", not_letters = "", words_full_df=words5){
# match is a string of 5 numbers
# - 0 is grey (no match)
# - 1 is yellow (exist)
# - 2 is green (position)
pattern_vec <- unlist(str_extract_all(match_pattern, ""))
if (length(pattern_vec) !=5){
stop("match_pattern not length 5")
}
word_vec <- unlist(str_extract_all(word_str, ""))
any_duplicate <- FALSE
if (length(unique(word_vec)) != length(word_vec)){
any_duplicate <- TRUE
}
grey <- pattern_vec == 0
yellow <- pattern_vec == 1
green <- pattern_vec == 2
# handle repeated letters
yes_letters <- word_vec[!grey]
all_not_letters <- c(word_vec[grey],not_letters)
all_not_letters <- all_not_letters[!all_not_letters %in% yes_letters]
possible_words <- five_solver(paste(yes_letters,collapse = ""),
sum(!grey),
paste(all_not_letters,collapse = ""),
words_full_df = words_full_df)
# match pattern
possible_words_matrix <- matrix(unlist(str_extract_all(possible_words, "")),
ncol = 5, byrow = T)
# green
if (any(green)){
green_matrix <- matrix(unlist(rep(word_vec[green], nrow(possible_words_matrix))),
nrow = nrow(possible_words_matrix),
byrow = TRUE)
green_check_matrix <- possible_words_matrix[,green] == green_matrix
green_check_vec <- apply(green_check_matrix,1,all)
possible_words_matrix <- possible_words_matrix[green_check_vec,]
}
# yellow
if (any(yellow)){
if (length(possible_words_matrix) == 5){
yellow_matrix <- word_vec[yellow]
yellow_check_matrix <- possible_words_matrix[yellow] == yellow_matrix
yellow_check_vec <- any(yellow_check_matrix)
possible_words_matrix <- possible_words_matrix[!yellow_check_vec]
} else {
yellow_matrix <- matrix(unlist(rep(word_vec[yellow], nrow(possible_words_matrix))),
nrow = nrow(possible_words_matrix),
byrow = TRUE)
yellow_check_matrix <- possible_words_matrix[,yellow] == yellow_matrix
yellow_check_vec <- apply(yellow_check_matrix,1,any)
possible_words_matrix <- possible_words_matrix[!yellow_check_vec,]
}
}
# grey second check if there are duplicated letters
if (any_duplicate){
if (length(possible_words_matrix) == 5){
grey_matrix <- word_vec[grey]
grey_check_matrix <- possible_words_matrix[grey] == grey_matrix
grey_check_vec <- any(yellow_check_matrix)
possible_words_matrix <- possible_words_matrix[!grey_check_vec]
} else {
grey_matrix <- matrix(unlist(rep(word_vec[grey], nrow(possible_words_matrix))),
nrow = nrow(possible_words_matrix),
byrow = TRUE)
grey_check_matrix <- possible_words_matrix[,grey] != grey_matrix
grey_check_vec <- apply(grey_check_matrix,1,all)
possible_words_matrix <- possible_words_matrix[grey_check_vec,]
}
}
# output
if(length(possible_words_matrix)==5){
out_word_list = paste(possible_words_matrix, collapse="")
} else {
out_word_list <- apply(possible_words_matrix,1,paste,collapse="")
}
return(out_word_list)
}
wordle_help_multi <- function(word_list, match_pattern_list){
if (length(word_list) != length(match_pattern_list)){
stop("length of word list and pattern list must be the same")
}
for (round_i in 1:length(word_list)){
if (round_i==1){
out_word_list <- wordle_help(word_list[round_i], match_pattern_list[round_i])
} else {
out_word_df <- words5 %>%
filter(word %in% out_word_list)
out_word_list <- wordle_help(word_list[round_i], match_pattern_list[round_i],
words_full_df = out_word_df)
}
}
return(out_word_list)
}
###### WORDLE SOLVER #############
# wordle_help_multi(
# c("spore","daunt","flick","clang","synod"),
# c("00002","00000","01000","01000","00000")
# )