Skip to content

Instantly share code, notes, and snippets.

Created March 19, 2012 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/2117190 to your computer and use it in GitHub Desktop.
Save anonymous/2117190 to your computer and use it in GitHub Desktop.
;; 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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment