Skip to content

Instantly share code, notes, and snippets.

@logaan
Created January 8, 2022 00:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logaan/eaabdabc2de7ad6ad1398aa25c0776e3 to your computer and use it in GitHub Desktop.
Save logaan/eaabdabc2de7ad6ad1398aa25c0776e3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
DICTIONARY = File.read('/Users/logaan/Desktop/scrabble2019.txt').split("\n").map{ |w| w.strip }
WORDLE_WORDS = DICTIONARY.filter{ |w| w.length == 5 }
LETTER_SCORES = WORDLE_WORDS.flat_map { |word| word.chars }.reduce({}) do |counts, character|
if counts.include?(character)
counts[character] = counts[character] + 1
else
counts[character] = 1
end
counts
end
UNIQUE_WORDS = WORDLE_WORDS.filter{ |w| w.chars.uniq.length == 5 }
def best_guess(words)
guess, score = words.reduce(["", 0]) do |best, challenger|
_, best_score = best
challenger_score = challenger.chars.map{ |c| LETTER_SCORES[c] }.sum
if challenger_score > best_score
[challenger, challenger_score]
else
best
end
end
guess
end
first = UNIQUE_WORDS
puts "first: " + best_guess(first)
# Suggests: AROSE
# Contains an S.
# Does not contain A, R, O, or E
# The S is not in the 4th position
second = UNIQUE_WORDS.filter { |w| w =~ /S/ && w !~ /[AROE]/ && w =~ /^...[^S]./ }
puts "second: " + best_guess(second)
# Suggests: LINTS
# Contains an S and an L.
# Does not contain A, R, O, or E
# The L is not in the 1st position
# The S is not in the 4th for 5th position
third = WORDLE_WORDS.filter { |w| w =~ /S/ && w =~ /L/ && w !~ /[AROEINT]/ && w =~ /^[^L]..[^S][^S]/ }
puts "third: " + best_guess(third)
# Suggests: SULLY
fourth = WORDLE_WORDS.filter do |w|
# Must contain a U
w =~ /U/ &&
# Must contain a L
w =~ /L/ &&
# Must not contain a any of: AROEINTY
w !~ /[AROEINTY]/ &&
# Must not contain a more than one L
w !~ /L.*L/ &&
# The 1st letter is S
# The 2nd letter is not U
# The 3rd letter is not L
# The 4th letter is not S or L
# The 5th letter is not S
w =~ /^S[^U][^L][^SL][^S]/
end
puts "fourth: " + best_guess(fourth)
# Suggests: Slump
# SUCCESS!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment