Skip to content

Instantly share code, notes, and snippets.

@jonathanyee
Forked from naush/draw_something.rb
Created March 25, 2012 06:43
Show Gist options
  • Save jonathanyee/2191903 to your computer and use it in GitHub Desktop.
Save jonathanyee/2191903 to your computer and use it in GitHub Desktop.
unless ARGV.size == 2
puts "USAGE: ruby guess.rb ACKRCKOOLCWZ 5"
exit 1
end
letters = ARGV[0].downcase.split(//).sort
length = ARGV[1].to_i
words = []
File.open('/usr/share/dict/words') do |file|
file.each do |line|
word = line.strip.downcase
words << word if word.length == length
end
end
def valid?(word, letters)
word_index = letters_index = 0
while word_index < word.length && letters_index < letters.length
if word[word_index] == letters[letters_index]
word_index = word_index + 1
end
letters_index = letters_index + 1
end
return word_index == word.length
end
guesses = []
words.each do |word|
guesses << word if valid?(word.split(//).sort, letters)
end
guesses.each do |guess|
puts guess
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment