Skip to content

Instantly share code, notes, and snippets.

@r38y
Last active December 12, 2015 09:29
Show Gist options
  • Save r38y/4751869 to your computer and use it in GitHub Desktop.
Save r38y/4751869 to your computer and use it in GitHub Desktop.
# 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)
@walterdavis
Copy link

Stunning! Love it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment