Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fronx/1321209 to your computer and use it in GitHub Desktop.
Save fronx/1321209 to your computer and use it in GitHub Desktop.
;; fronx's solution to Love Triangle
;; https://4clojure.com/problem/127
(fn [field]
(let [
bit-max (fn [int]
(loop [n 1 i 0]
(if (>= int n)
(recur (* 2 n) (inc i))
i)))
max-bit-max (fn [ints] (bit-max (apply max ints)))
flip-down #(reverse %)
flip-right (fn [triangle]
(map #(bit-shift-left % (- (max-bit-max triangle) (bit-max %)))
triangle))
tri-at? (fn [triangle x y field]
(if (>= (count field) (+ y (count triangle)))
(let [triangle (vec (map #(bit-shift-left % x) triangle))
matchfield (subvec field y)]
(every? true?
(map (fn [tri x]
(= tri (bit-and tri x)))
triangle
matchfield)))))
bit-count (fn [ints] (reduce + (map #(Integer/bitCount %) ints)))
wedge (fn [size]
(take size ((fn wedge
([] (wedge [1]))
([prev] (lazy-cat prev
(wedge [(+ 1 (bit-shift-left (peek prev) 1))])
))))))
fir (fn [size]
(map #(bit-or % (bit-shift-left %2 size))
(flip-right (wedge size))
(concat [0] (wedge (dec size)))))
play-button (fn [size]
(concat (wedge size)
(flip-down (wedge (dec size)))))
triangles (fn [size]
(let [a (wedge size)
b (flip-right a)
c (flip-down a)
d (flip-down b)
e (fir size)
f (flip-down e)
g (play-button size)
h (flip-right g)]
(set [a b c d e f g h])))
x-max (max-bit-max field)]
(loop [size 2 biggest 0]
(let [found (for [tri (triangles size)
x (range 0 x-max)
y (range 0 (inc (- (count field) (count tri))))
:when (tri-at? tri x y field)]
(bit-count tri))]
(if (or (empty? found)
(<= (apply max found) biggest))
(if (> biggest 0) biggest)
(recur (inc size) (apply max found)))))))
@theophani
Copy link

I like this indentation style i.e. right aligning the function names.

@nisc
Copy link

nisc commented Oct 28, 2011

Confirmed working, but isn't there a way to have more lazy-cats?

@fronx
Copy link
Author

fronx commented Oct 28, 2011

@nisc I invited more lazy cats. Still waiting for them to respond.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment