Last active
December 12, 2015 09:29
-
-
Save r38y/4751869 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requires Ruby 1.9 and OS X | |
# The letters in your game... include repeats | |
game_letters = %w() | |
# Words that have been played already | |
played = %w() | |
def to_hash(letters) | |
letters.inject({}) do |h,l| | |
h[l] ||= 0 | |
h[l] += 1 | |
h | |
end | |
end | |
game_hash = to_hash(game_letters) | |
count = 0 | |
matches = [] | |
IO.foreach('/usr/share/dict/words') do |line| | |
word = line.downcase.strip | |
next if played.include?(word) | |
letters = word.split('') | |
letters_hash = to_hash(letters) | |
answers = letters_hash.to_a.map do |pair| | |
game_count = game_hash[pair.first] | |
game_count && game_count >= pair.last | |
end | |
if answers.all? | |
matches << word | |
end | |
end | |
matches.sort! do |x,y| | |
y.size <=> x.size | |
end | |
puts matches.slice(0, 50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stunning! Love it!