Skip to content

Instantly share code, notes, and snippets.

;; 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 ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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]
;; 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)))
@amalloy
amalloy / gist:1214703
Created September 13, 2011 18:55 — forked from mwmitchell/gist:1214682
rate-filter.clj
(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)
;; 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)))))))
@amalloy
amalloy / joly-4clojure-solution101.clj
Created August 31, 2011 17:54 — forked from jamesoly/joly-4clojure-solution101.clj
Memoized version of 4clojure problem 101
(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)]
@amalloy
amalloy / abundantsums.clj
Created August 30, 2011 19:31
Confusing results
;; 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"
;; 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]]
@amalloy
amalloy / gist:1177884
Created August 29, 2011 06:28 — forked from zolrath/gist:1177848
Clojure 1.2.1 vs 1.3.0-beta2 Question
(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)))
(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))))))