Created
November 4, 2012 13:40
-
-
Save barelyknown/4011968 to your computer and use it in GitHub Desktop.
Calculate All Moves For A Word in Letterpress Board
This file contains hidden or 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 a board_tiles method that returns an array of objects that respond to the letter method | |
def board_tiles_by_letter | |
by_letter = {} | |
('A'..'Z').each { |letter| by_letter[letter] = [] } | |
board_tiles.each { |board_tile| by_letter[board_tile.letter] << board_tile } | |
by_letter | |
end | |
def every_play(word) | |
word_letters = word.chars.to_a | |
with_duplicates = board_tiles_by_letter[word_letters.shift].product(*word_letters.map { |letter| board_tiles_by_letter[letter] }) | |
with_duplicates.select { |play| play.uniq.count == play.count } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment