Skip to content

Instantly share code, notes, and snippets.

@mariusae
Created November 4, 2012 21:36
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 mariusae/4013882 to your computer and use it in GitHub Desktop.
Save mariusae/4013882 to your computer and use it in GitHub Desktop.
Letterpress solver
(* ocamlbuild letterpress.native *)
let rec mkword ?(i=0) ?(l=[]) w =
if i = (String.length w) then
let l' = List.map Char.lowercase l in
List.fast_sort Char.compare l'
else
mkword ~i:(i+1) ~l:(w.[i]::l) w
let rec contains b w = match (b,w) with
| (x::b', y::_) when x < y -> contains b' w
| (x::b', y::w') when x = y -> contains b' w'
| (_, []) -> true
| _ -> false
let () =
let board = Sys.argv.(1) in
let b = mkword board in
try
while true do
let word = input_line stdin in
let w = mkword word in
if contains b w then
Printf.printf "%s\n" word
else ()
done
with
End_of_file -> ()
@mariusae
Copy link
Author

mariusae commented Nov 4, 2012

% time ruby /tmp/slowword.rb  > /tmp/slow
        6.29 real         6.25 user         0.00 sys
% time letterpress.native asviqaazotvepjffysjrrasdl < /usr/share/dict/words > /tmp/fast
        0.26 real         0.24 user         0.00 sys

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