-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path059brute.clj
26 lines (22 loc) · 1.01 KB
/
059brute.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(defn number-of-ascii-chars [s]
(count (filter #(or (and (<= (int \a) %) (<= % (int \z)))
(and (<= (int \A) %) (<= % (int \Z)))
(= (int \newline) %)
(= (int \space) %))
(map int s))))
(defn decrypt [s key]
(map bit-xor s (cycle key)))
(defn euler059 [file]
(let [message (map #(Integer/parseInt %) (re-seq #"\d+" (slurp file)))
lower (map int "abcdefghijklmnopqrstuvwxyz")
best-match (first (sort-by second >
(for [a lower
b lower
c lower]
[(str (char a) (char b) (char c))
(number-of-ascii-chars (decrypt message [a b c]))])))
decrypted (decrypt message (map int (first best-match)))]
(println "key: " best-match)
(println "message: " (apply str (map char decrypted)))
(println (reduce + decrypted))))
(time (euler059 "cipher1.txt"))