Forked from anonymous/ummels-4clojure-solution127.clj
Created
March 19, 2012 15:58
Revisions
-
ummels revised this gist
Mar 22, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ ln #(loop [r 1 v %] (if (< v 2) r (recur (inc r) (quot v 2)))) m (apply max (map ln mine)) lmax (fn [xs] ; treats nil as -infinity (let [a (first xs) r1 (rest xs) b (first r1) r2 (rest r1)] (cond (empty? r1) a (nil? a) (recur r1) (nil? b) (recur (cons a r2)) -
ummels revised this gist
Mar 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ (map * [i i] lft) (map * [(- k i) (- k i)] rt) (map * [b b] [x y])))] (when (every? mineral? points) (count points))))] (lmax (for [x (range n) y (range m) :when (mineral? [x y]) -
ummels revised this gist
Mar 20, 2012 . 1 changed file with 7 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,12 +28,10 @@ (map * [(- k i) (- k i)] rt) (map * [b b] [x y])))] (if (every? mineral? points) (count points) nil)))] (lmax (for [x (range n) y (range m) :when (mineral? [x y]) d [[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]]] (loop [r nil w 1] (if-let [a (area [x y] d w)] (recur a (inc w)) r)))))) -
ummels revised this gist
Mar 20, 2012 . 1 changed file with 9 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,10 +28,12 @@ (map * [(- k i) (- k i)] rt) (map * [b b] [x y])))] (if (every? mineral? points) (count points) nil)))] (-> (for [x (range n) y (range m) :when (mineral? [x y]) d [[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]]] (loop [r nil w 1] (if-let [a (area [x y] d w)] (recur a (inc w)) r))) lmax))) -
ummels revised this gist
Mar 20, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,8 +14,8 @@ mineral? (fn [[x y]] (and (>= (min x y) 0) (< x n) (loop [i y r (mine x)] (if (zero? i) (odd? r) (recur (dec i) (quot r 2)))))) area (fn [corner [x y] w] (let [sgn #(max (min % 1) -1) lft [(sgn (+ x y)) (sgn (- y x))] ; [x y] turned ccw by 45 -
ummels revised this gist
Mar 20, 2012 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,9 +12,8 @@ (nil? b) (recur (cons a r2)) :else (recur (cons (max a b) r2))))) mineral? (fn [[x y]] (and (>= (min x y) 0) (< x n) (loop [i y n (mine x)] (if (zero? i) (odd? n) (recur (dec i) (quot n 2)))))) area (fn [corner [x y] w] -
ummels revised this gist
Mar 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ (let [n (count mine) ln #(loop [r 1 v %] (if (< v 2) r (recur (inc r) (quot v 2)))) m (apply max (map ln mine)) lmax (fn [xs] ; treats nil as -infinity (let [a (first xs) r1 (rest xs) b (second xs) r2 (rest (rest xs))] (cond (empty? r1) a (nil? a) (recur r1) -
ummels revised this gist
Mar 20, 2012 . 1 changed file with 14 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,6 +5,12 @@ (let [n (count mine) ln #(loop [r 1 v %] (if (< v 2) r (recur (inc r) (quot v 2)))) m (apply max (map ln mine)) lmax (fn [xs] (let [a (first xs) r1 (rest xs) b (second xs) r2 (rest (rest xs))] (cond (empty? r1) a (nil? a) (recur r1) (nil? b) (recur (cons a r2)) :else (recur (cons (max a b) r2))))) mineral? (fn [[x y]] (and (>= x 0) (< x (count mine)) @@ -22,10 +28,11 @@ (map * [i i] lft) (map * [(- k i) (- k i)] rt) (map * [b b] [x y])))] (if (every? mineral? points) (count points) nil)))] (lmax (for [x (range n) y (range m) :when (mineral? [x y]) d [[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]]] (loop [r nil w 1] (if-let [a (area [x y] d w)] (recur a (inc w)) r)))))) -
ummels revised this gist
Mar 19, 2012 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,13 +5,13 @@ (let [n (count mine) ln #(loop [r 1 v %] (if (< v 2) r (recur (inc r) (quot v 2)))) m (apply max (map ln mine)) mineral? (fn [[x y]] (and (>= x 0) (< x (count mine)) (>= y 0) (loop [i y n (mine x)] (if (zero? i) (odd? n) (recur (dec i) (quot n 2)))))) area (fn [corner [x y] w] (let [sgn #(max (min % 1) -1) lft [(sgn (+ x y)) (sgn (- y x))] ; [x y] turned ccw by 45 rt [(sgn (- x y)) (sgn (+ x y))] ; [x y] turned cw by 45 @@ -22,10 +22,10 @@ (map * [i i] lft) (map * [(- k i) (- k i)] rt) (map * [b b] [x y])))] (if (every? mineral? points) (count points) 0))) res (apply max (for [x (range n) y (range m) d [[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]] w (range (max n m))] (area [x y] d w)))] (if (> res 2) res nil))) -
ummels revised this gist
Mar 19, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,6 +26,6 @@ res (apply max (for [x (range n) y (range m) d [[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]] w (range (max n m))] (area mine [x y] d w)))] (if (> res 2) res nil))) -
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ ;; ummels's solution to Love Triangle ;; https://4clojure.com/problem/127 (fn [mine] (let [n (count mine) ln #(loop [r 1 v %] (if (< v 2) r (recur (inc r) (quot v 2)))) m (apply max (map ln mine)) mineral? (fn [mine [x y]] (and (>= x 0) (< x (count mine)) (>= y 0) (loop [i y n (mine x)] (if (zero? i) (odd? n) (recur (dec i) (quot n 2)))))) area (fn [mine corner [x y] w] (let [sgn #(max (min % 1) -1) lft [(sgn (+ x y)) (sgn (- y x))] ; [x y] turned ccw by 45 rt [(sgn (- x y)) (sgn (+ x y))] ; [x y] turned cw by 45 points (for [k (range (inc w)) i (range (inc k)) b (if (and (some zero? [x y]) (< k w)) [0 1] [0])] (map + corner (map * [i i] lft) (map * [(- k i) (- k i)] rt) (map * [b b] [x y])))] (if (every? #(mineral? mine %) points) (count points) 0))) res (apply max (for [x (range n) y (range m) d [[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]] w (range (* 2 n))] (area mine [x y] d w)))] (if (> res 2) res nil)))