-
Notifications
You must be signed in to change notification settings - Fork 1
/
wordle_solver.rb
121 lines (97 loc) · 3.06 KB
/
wordle_solver.rb
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
require "Set"
require './words.rb'
def main
included_words = Words.included_words
words = []
included_words.each do |word|
word = word.strip
if word.length == 5
words << word.downcase
end
end
frequencies = letter_frequencies(words)
word_scores = Hash.new(0)
# USAGE
# guess the top word (arose usually)
# if there are grey letters, add them to the array
grey_letters = []
# if there are green letters, place them in the array (has to be in the 5 char array)
green_letters = ['', '', '', '', '']
# if there are yellow letters, add them to the array
yellow_letters_arrays = []
loop do
words.each do |word|
word_scores[word] = score_word(word, frequencies, grey_letters, green_letters, yellow_letters_arrays)
end
sorted_scores = word_scores.sort_by {|k, v| v}
values = sorted_scores.map {|score| score[0]}
puts "Your best guesses to start off are:"
p values.last(10).reverse
puts "add to the grey letters: #{grey_letters} \n"
new_grey_letters = gets.chomp
grey_letters << new_grey_letters.split(//)
grey_letters.flatten!
p grey_letters
puts "change the yellow letters (_ for empty): #{yellow_letters_arrays} \n"
new_yellow_letters = gets.chomp
new_yellow_letters = new_yellow_letters.split(//)
# if new_yellow_letters == ""
# new_yellow_letters = ['', '', '', '', '']
# end
yellow_letters_arrays << new_yellow_letters.map { |x| x == '_' ? '' : x }
p yellow_letters_arrays
puts "change the green letters (_ for empty): #{green_letters} \n"
new_green_letters = gets.chomp
# if new_green_letters == ""
# new_green_letterss = ['', '', '', '', '']
# end
green_letters = new_green_letters.split(//)
green_letters = green_letters.map { |x| x == '_' ? '' : x }
p green_letters
end
end
def letter_frequencies(words)
freq = Hash.new(0)
words.each do |word|
word.split(//).each do |c|
freq[c] = freq[c] + 1
end
end
return freq
end
def unique_characters(word)
Set.new(word.split(//)).length
end
def matches_green_letters?(word, green_letters)
(word.length).times do |i|
if green_letters[i] != '' && green_letters[i] != word[i]
return false
end
end
return true
end
def matches_yellow_letters?(word, yellow_letters_arrays)
yellow_letters_arrays.each do |yellow_letters_array|
stripped_letters = yellow_letters_array.clone
stripped_letters.delete_if(&:empty?)
stripped_letters.each do |c|
return false unless word.include?(c)
end
(word.length).times do |i|
if yellow_letters_array[i] != '' && yellow_letters_array[i] == word[i]
return false
end
end
end
return true
end
def score_word(word, letter_frequency, grey_letters, green_letters, yellow_letters_arrays)
score = 0
word.split(//).each do |c|
return 0 if grey_letters.include?(c) || !matches_green_letters?(word, green_letters) || !matches_yellow_letters?(word, yellow_letters_arrays)
score += letter_frequency[c]
end
score = score * unique_characters(word)
return score
end
main