Skip to content

Instantly share code, notes, and snippets.

@stevej
Created November 2, 2012 22:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevej/4004751 to your computer and use it in GitHub Desktop.
Save stevej/4004751 to your computer and use it in GitHub Desktop.
Letterpress board searcher
#/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
@mariusae
Copy link

mariusae commented Nov 4, 2012

Here's one in ocaml; 10x faster! ~ https://gist.github.com/4013882

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