AvataroftheSun

Scrabble Chains

Nov 23rd, 2017
267
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'net/http'
  2.  
  3. # Load from a file first, web second
  4. if File.exist? "dictionary"
  5.   text = File.read("dictionary")
  6. else
  7.   url = "https://pastebin.com/raw/ejKQvqMa"
  8.   text = Net::HTTP.get(URI(url))
  9.   File.write("dictionary", text)
  10. end
  11. @words = text.split("\n").map(&:chomp)
  12. puts "Dictionary loaded"
  13.  
  14. # Create the spelling map and populate it word by word
  15. @spellmap = {}
  16. @spellmap.instance_eval do
  17.   # Adds a word to the spelling map
  18.   # By default, this adds a complete word to the base map. This method is called
  19.   # recursively to add smaller chunks to lower levels of the map.
  20.   # @param word the word to add to the specified lettermap
  21.   # @param baseword (optional) the base word that triggered the add action
  22.   # @param lettermap (optional) the lettermap to add word to
  23.   def add_word(word, baseword = word, lettermap=self)
  24.     return nil unless baseword # Don't do anything without a base
  25.     if word.empty? # Word is empty, assign it and return the current map
  26.       lettermap[:word] = baseword
  27.       return lettermap
  28.     end
  29.     i0 = word[0] # Get the first character of the chunk
  30.     child_map = lettermap[i0] ||= {} # Make a new map if one doesn't exist
  31.     child_map[:parent] = lettermap # Set parent property
  32.     add_word(word[1..-1], baseword, child_map) # Add the resulting chunk to the child map
  33.   end
  34.  
  35.   # Retrieve a word from the spelling map. This calls itself recursively as it
  36.   # traverses the word to search for
  37.   # @param word the word to search for in the specified lettermap
  38.   # @param lettermap (optional) the lettermap to search
  39.   def get_word(word, lettermap=self)
  40.     return nil unless word && lettermap
  41.     return get_word(word[1..-1], lettermap[word[0]]) unless word.empty?
  42.     lettermap
  43.   end
  44. end
  45.  
  46. @words.each { |word| @spellmap.add_word(word) }
  47. puts "Dictionary processed"
  48. @processing = @words.select { |word| word.length == 2 }
  49. @longest = nil
  50.  
  51. until @processing.empty?
  52.   chunk = @processing.pop
  53.   chunk = @spellmap.get_word(chunk) if chunk.class == String
  54.   next unless chunk && chunk[:word]
  55.   if @longest.nil? || chunk[:word].size > @longest[:word].size
  56.     @longest = chunk
  57.     puts "New longest word: #{@longest[:word]}"
  58.   end
  59.   chunk.each_pair do |k, v|
  60.     next unless k.length == 1 && v.class == Hash
  61.     @processing << v unless v[:word].nil?
  62.   end
  63.   ('a'..'z').each { |letter| @processing << (letter + chunk[:word]) }
  64. end
  65.  
  66. puts @longest[:word]
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×