Skip to content

Instantly share code, notes, and snippets.

@trevvvy
Created March 9, 2022 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trevvvy/779ecfcb8d0188d87696dfabaca56a86 to your computer and use it in GitHub Desktop.
Save trevvvy/779ecfcb8d0188d87696dfabaca56a86 to your computer and use it in GitHub Desktop.
dictionary = File.read('dictionary.txt').split("\n")
five_letter_words = dictionary.select{|word| word.length == 5}
most_common_letters = five_letter_words.inject({}) do |result, word|
letters_array = word.chars
letters_counts = letters_array.tally
letters_counts.to_a.each_with_index do |(letter, count), index|
# next if letter == "s" && index == 4
result[letter] ||= 0
result[letter] += count
end
result
end
letter_scores = most_common_letters.sort_by{|k,v| -v}.to_h
scored_words = five_letter_words.inject({}) do |hash, word|
word_score = word.chars.inject(0) do |score, letter|
score += letter_scores[letter]
score
end
hash[word] = word_score
hash
end
top_words = scored_words.sort_by{|k,v| -v}.map{|word, score| word}
top_single_letter_words = top_words.select{|word| word.chars.uniq.length == 5}
best_pairings = []
top_single_letter_words.each do |word|
second_word = top_single_letter_words.detect do |second_word|
next if best_pairings.flatten.include?(second_word)
(second_word.chars - word.chars).length == 5
end
third_word = top_single_letter_words.detect do |third_word|
next if best_pairings.flatten.include?(second_word)
next if best_pairings.flatten.include?(third_word)
(third_word.chars - word.chars - second_word.chars).length == 5
end
next unless third_word
best_pairings << [word, second_word, third_word]
break if best_pairings.length == 10
end
best_pairings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment