Mercurial > coderloop
comparison src/decrypt.clj @ 0:307a81e46071 tip
initial committ
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Oct 2011 01:17:49 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:307a81e46071 |
---|---|
1 (ns coderloop.decrypt | |
2 (:use [clojure.contrib | |
3 [duck-streams :only [read-lines]] | |
4 [seq :only [find-first]] | |
5 [string :only [map-str]] | |
6 [combinatorics :only [cartesian-product]]]) | |
7 (:use rlm.shell-inspect) | |
8 (:use [clojure [string :only [split]]]) | |
9 (:import Crypt)) | |
10 | |
11 (def t "/home/r/coderloop-test/test.txt") | |
12 | |
13 (defn strcat [coll] | |
14 (apply str coll)) | |
15 | |
16 (def numerals [0 2 4 8 ]) | |
17 | |
18 (defn crypt [salt s] | |
19 (Crypt/crypt salt s)) | |
20 | |
21 (defn cross-text [text] | |
22 (let [symbols (filter | |
23 (fn [s] (<= (count s) 8)) | |
24 (split (.toLowerCase text) #"[^a-z]+"))] | |
25 (filter | |
26 (fn [s] | |
27 (let [len (count s)] | |
28 (and | |
29 (>= len 5) | |
30 (<= len 8)))) | |
31 (map (partial apply str) | |
32 (cartesian-product symbols numerals symbols))))) | |
33 | |
34 (defn process-file [f] | |
35 (let [file (read-lines f) | |
36 [salt pass] (map (partial apply str) (split-at 2 (first file))) | |
37 text (strcat (interleave (repeat \newline) (rest file)))] | |
38 (find-first (fn [s] (= (str salt pass) (crypt salt s))) | |
39 (cross-text text)))) | |
40 | |
41 | |
42 (if (command-line?) | |
43 (println (process-file (first *command-line-args*)))) |