Permalink
Browse files

Solved 98.

  • Loading branch information...
1 parent a3c3721 commit 43250a643ce8b0e4658978a2baa744c6652f7adf @darrenaustin committed Oct 7, 2011
Showing with 40 additions and 1 deletion.
  1. +0 −1 README.md
  2. +40 −0 src/dma/euler/p098.clj
View
@@ -46,7 +46,6 @@ it in Ruby. Here is my migration TODO list. Problems I have solved
in Ruby, but not yet in Clojure:
* 96
-* 98
* 99
* 111
* 114
@@ -0,0 +1,40 @@
+(ns dma.euler.p098
+ (:use dma.euler.numeric)
+ (:use clojure.contrib.combinatorics))
+
+(defn has-repeating-digits? [n]
+ (not= (count (distinct (str n)))
+ (count (str n))))
+
+(defn anagrams [words]
+ (filter #(< 1 (count %))
+ (vals (group-by sort words))))
+
+(defn num-digit-sub [w1 w2 n]
+ (number (map (zipmap w1 (str n)) w2)))
+
+(defn square-anagram-pair [w1 w2 squares]
+ (let [square? (set squares)]
+ (some (fn [s1]
+ (let [s2 (num-digit-sub w1 w2 s1)]
+ (if (square? s2)
+ (max s1 s2))))
+ squares)))
+
+(defn max-square-pair [words squares]
+ (let [matches (keep identity (map #(square-anagram-pair (first %) (second %) squares)
+ (combinations words 2)))]
+ (when (seq matches)
+ (apply max matches))))
+
+(defn solution {:answer 18769} []
+ (let [words (re-seq #"\w+" (slurp "data/words.txt"))
+ anagrams (reverse (sort-by #(count (first %)) (anagrams words)))
+ max-chars (count (ffirst anagrams))
+ squares (group-by num-digits
+ (remove has-repeating-digits?
+ (take-while-< (exp 10 max-chars) (map square (range)))))]
+ (first
+ (keep identity
+ (map #(max-square-pair % (squares (count (first %)))) anagrams)))))
+

0 comments on commit 43250a6

Please sign in to comment.