Created
November 2, 2012 22:28
-
-
Save stevej/4004751 to your computer and use it in GitHub Desktop.
Letterpress board searcher
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
#/usr/bin/env ruby | |
# A brute-force letterpress board searcher. It takes about 90 seconds to search a board. | |
# | |
# Good uses: train on your vocabulary with boards you lost on. | |
# | |
# LAZY AND STUPID CHEATERS: DO NOT USE THIS FOR CHEATING. CHEATERS NEVER WIN. | |
class BoardSearcher | |
attr_reader :board | |
def initialize(unsorted_board) | |
@board = unsorted_board.sort | |
end | |
def contains(unsorted_candidate) | |
candidate = unsorted_candidate.sort | |
candidate_index = 0 | |
@board.each do |letter| | |
candidate_index += 1 if letter == candidate[candidate_index] | |
end | |
(candidate_index == candidate.length) ? true : false | |
end | |
end | |
if __FILE__ == $0 | |
filename = ARGV[0] || "/usr/share/dict/words" | |
board = (ARGV[1] || "asviqaazotvepjffysjrrasdl").chars.to_a | |
searcher = BoardSearcher.new(board) | |
File.foreach(filename) do |line| | |
candidate = line.downcase.chomp | |
if searcher.contains(candidate.chars.to_a) | |
puts candidate | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's one in ocaml; 10x faster! ~ https://gist.github.com/4013882