Skip to content

Instantly share code, notes, and snippets.

;; ariarule's solution to Universal Computation Engine
;; https://4clojure.com/problem/121
(fn uce [formula]
(fn [z]
(letfn [
(replacer [x]
(if (list? x)
(apply (get {'/ / '+ + '- - '* *} (first x))
(map replacer (rest x)))
;; ariarule's solution to Oscilrate
;; https://4clojure.com/problem/144
(fn [x & f]
(letfn [(osnx [x & f]
(lazy-seq
(let [c ((first f) x)]
(cons c (apply (partial osnx c) (concat (rest f) (list (first f))))))))]
(lazy-seq
(cons x (apply (partial osnx x) f)))))
;; ariarule's solution to Prime Sandwich
;; https://4clojure.com/problem/116
(fn [n]
(letfn [(is-prime? [n]
(if (= 2 n)
true
(if (or (= 1 n) (and (even? n) (> 2)))
false
(not (reduce #(or %1 (integer? (/ n %2))) false (range 3 n))))))]
;; ariarule's solution to Win at Tic-Tac-Toe
;; https://4clojure.com/problem/119
(fn [player board]
(let [vboard (apply concat board)
s (range 0 3)
positions (mapcat #(map (fn [x] [% x]) s) s)
rows (partition 3 positions)
columns [(map first rows) (map second rows) (map #(nth % 2) rows)]
wins (map set (concat rows columns ['([0 0] [1 1] [2 2]) '([0 2] [1 1] [2 0])]))
;; ariarule's solution to Pairwise Disjoint Sets
;; https://4clojure.com/problem/153
(fn [s]
(every? #(< % 2)
(map
#(reduce
(fn [n x]
(if (not (= 0 x))
(inc n)
;; ariarule's solution to Global take-while
;; https://4clojure.com/problem/114
(fn [n p s]
(take (dec (count (take-while #(< % n)
(reduce #(let [n (if %2
(inc (last %1))
(last %))]
(conj %1 n))
[0]
;; ariarule's solution to Lazy Searching
;; https://4clojure.com/problem/108
(fn [l & ls]
(let [lz (conj ls l)]
(first
(filter identity
(map
(fn [ocl] (reduce #(and %1 %2) true ocl))
(map
;; ariarule's solution to Digits and bases
;; https://4clojure.com/problem/137
(fn convert-base
([n b]
(if (= n 0) '(0)
(let [p (reverse (take-while #(<= % n) (map #(Math/pow b %) (range))))]
(convert-base '() p n ))))
([l p r]
(if (= (count p) 1)
;; ariarule's solution to Analyze a Tic-Tac-Toe Board
;; https://4clojure.com/problem/73
(fn analyze-tic-tac-toe [board]
(let [things-to-check
[{:step (fn [z] [(first z),(inc (second z))])
:start [[0,0] [1,0] [2,0]]}
{:step (fn [z] [(inc (first z)),(second z)])
:start [[0,0] [0,1] [0,2]]}
{:step (fn [z] [(inc (first z)),(inc (second z))])
;; ariarule's solution to Write Roman Numerals
;; https://4clojure.com/problem/104
(fn to-roman [n]
(let [sep-rom (take-last 4 (map #(Integer/parseInt (str %)) (str "000" n)))
rom-m (first sep-rom)
rom-less (rest sep-rom)]
(apply str
(concat (repeat rom-m \M)
(mapcat