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
;; amalloy's solution to Analyze Reversi | |
;; https://4clojure.com/problem/124 | |
(fn [board color] | |
(let [;; we'll need to verify that we only "capture" enemy pieces | |
enemy? #{('{b w, w b} color)} | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; board/position related constants ;;; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
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
;; amalloy's solution to For Science! | |
;; https://4clojure.com/problem/117 | |
(fn [board] | |
(let [names {\space :open | |
\# :wall | |
\M :goal | |
\C :start} | |
board (vec (for [row board] | |
(vec (for [col row] |
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
;; amalloy's solution to Universal Computation Engine | |
;; https://4clojure.com/problem/121 | |
(fn [formula] | |
(fn [values] | |
((fn compute [x] | |
(if (seq? x) | |
(let [[op & args] x] | |
(apply ({'+ + '/ / '- - '* *} op) | |
(map compute args))) |
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
(defn remove-out-of-range-rates [rates min-rate max-rate] | |
(let [out-of-range? #(not (<= min-rate (:total_rate %) max-rate))] | |
(for [{r :rates :as item} rates | |
:let [allowed (remove out-of-range? r)] | |
:when (seq allowed)] | |
(assoc item :rates allowed)))) | |
(remove-out-of-range-rates [{:rates [{:total_rate 10} {:total_rate 15}]} | |
{:rates [{:total_rate 12} {:total_rate 9}]}] | |
10 15) |
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
;; amalloy's solution to Global take-while | |
;; https://4clojure.com/problem/114 | |
(fn f [n pred coll] | |
(lazy-seq | |
(when-let [[x & xs] (seq coll)] | |
(let [n (if (pred x), (dec n), n)] | |
(when-not (zero? n) | |
(cons x (f n pred xs))))))) |
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
(def levdist | |
(let [lev (atom nil) | |
impl | |
(fn [s t] | |
(let [lev @lev] | |
(cond | |
(empty? s) (count t) | |
(empty? t) (count s) | |
:else (let [ns (rest s) | |
nt (rest t)] |
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
;; As it turns out, A is much slower than B in some cases, but faster in others! Fun. | |
(time (sum-divisorsA 2000000)) => "Elapsed time: 3766.863 msecs" | |
(time (sum-divisorsB 2000000)) => "Elapsed time: 113.786 msecs" | |
(time (sum-divisorsA 200000)) => "Elapsed time: 3.233 msecs" | |
(time (sum-divisorsB 200000)) => "Elapsed time: 8.436 msecs" | |
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
;; amalloy's solution to Levenshtein Distance | |
;; https://4clojure.com/problem/101 | |
(letfn [(iters [n f start] | |
(take n (map second | |
(iterate f start))))] | |
(fn [s t] | |
(let [m (inc (count s)), n (inc (count t)) | |
first-row (vec (range m)) | |
matrix (iters n (fn [[j row]] |
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
(letfn [(divides? [num div] | |
(zero? (mod num div))) | |
(divisors [n] | |
(filter #(divides? n %) (range 1 n))) | |
(d [n] (apply + (divisors n)))] | |
(apply + | |
(for [i (range 1 1000) | |
:let [di (d i)] | |
j (range 1 i) | |
:when (and (= di j) (= i (d j))) |
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
(defn partition-with | |
"Applies f to each value in coll, splitting it each time f returns true. | |
Returns a lazy seq of partitions." | |
[f coll] | |
(lazy-seq | |
(when-let [s (seq coll)] | |
(let [[head tail] (split-with (complement f) (rest coll))] | |
(cons (cons (first coll) head) | |
(partition-with f tail)))))) |