Forked from anonymous/fronx-4clojure-solution127.clj
Created
October 27, 2011 23:38
-
-
Save fronx/1321209 to your computer and use it in GitHub Desktop.
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 characters
;; 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))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@nisc I invited more lazy cats. Still waiting for them to respond.