ajet.core coming soon to a repository near you?!

This commit is contained in:
Adam Jeniski 2023-12-17 10:19:28 -05:00
parent 8d236e36ea
commit 872bc909ff
2 changed files with 50 additions and 38 deletions

View File

@ -30,12 +30,12 @@
(defmacro comp> (defmacro comp>
"comp, but fn-args are applied from left to right" "comp, but fn-args are applied from left to right"
[& ls] (let [r (reverse ls)] [& ls] (let [r# (reverse ls)]
`(comp ~@r))) `(comp ~@r#)))
(defmacro static-fn (defmacro static-fn
"wraps java static methods in a lambda so they can be passed as function objects" "wraps java static methods in a lambda so they can be passed as function objects"
[f] `#(~f %)) [f] `(fn [v#] (~f v#)))
(defn mapvf (defn mapvf
"returns a function that does mapv over f when called with a coll" "returns a function that does mapv over f when called with a coll"
@ -46,7 +46,7 @@
([f] #(reduce f %)) ([f] #(reduce f %))
([f init] #(reduce f init %))) ([f init] #(reduce f init %)))
(defmacro mfn (defmacro fn-m
"like fn but memoizes return values, including recursive calls" "like fn but memoizes return values, including recursive calls"
[name arglist & body] [name arglist & body]
`(let [dp# (atom {}) `(let [dp# (atom {})
@ -57,18 +57,31 @@
res#)))] res#)))]
f#)) f#))
(defmacro defmfn (defmacro defn-m
"like defn but for a memoized fn, see ajet.core/mfn" "like defn but for a memoized fn, see ajet.core/fn-m"
[name & args] [name & args]
`(def ~name (mfn ~name ~@args))) `(def ~name (fn-m ~name ~@args)))
(defmacro log [& body]
(let [exprs# (map (fn [e#]
`(let [e-res# ~e#]
(println e-res# "\t\texpr:" '~e#)
e-res#)) body)]
`(do ~@exprs#
nil)))
(comment (comment
(map (static-fn Long/parseLong) ["123" "456"]) (map (static-fn Long/parseLong) ["123" "456"])
input-cache input-cache
(defmfn fib [x] (log (+ 5 5)
(- 2 1))
(defn-m fib [x]
(if (< x 2) (if (< x 2)
x x
(+ (fib (dec x)) (+ (fib (dec x))
(fib (- x 2))))) (fib (- x 2)))))
(fib 200N)) (fib 20000N)
;
)

View File

@ -1,7 +1,7 @@
(ns day12 (ns day12
(:require (:require
[clojure.string :as str] [clojure.string :as str]
[core :refer [get-puzzle-input mfn]])) [core :refer [get-puzzle-input fn-m]]))
(defn parse-line [line] (-> (str/split line #" ") (defn parse-line [line] (-> (str/split line #" ")
(update 1 #(->> (str/split % #",") (update 1 #(->> (str/split % #",")
@ -10,37 +10,36 @@
(defn ans [dots blocks] (defn ans [dots blocks]
(let (let
[len-blocks (count blocks) [len-blocks (count blocks)
f f (fn-m f [i bi current]
(mfn f [i bi current] (let [dot-i (when (< i (count dots)) (get dots i))]
(let [dot-i (when (< i (count dots)) (get dots i))] (if (= i (count dots))
(if (= i (count dots)) (cond (and (= bi len-blocks)
(cond (and (= bi len-blocks) (= current 0))
(= current 0)) 1
1 (and (= bi (dec len-blocks))
(and (= bi (dec len-blocks)) (= current (get blocks bi)))
(= current (get blocks bi))) 1
1 :else
:else 0)
0) (->> [\. \#]
(->> [\. \#] (map #(if (or (= dot-i %)
(map #(if (or (= dot-i %) (= dot-i \?))
(= dot-i \?)) (cond (and (= % \.)
(cond (and (= % \.) (= current 0))
(= current 0)) (f (inc i) bi 0)
(f (inc i) bi 0)
(and (= % \.) (and (= % \.)
(> current 0) (> current 0)
(< bi len-blocks) (< bi len-blocks)
(= (get blocks bi) current)) (= (get blocks bi) current))
(f (inc i) (inc bi) 0) (f (inc i) (inc bi) 0)
(= % \#) (= % \#)
(f (inc i) bi (inc current)) (f (inc i) bi (inc current))
:else 0) :else 0)
0)) 0))
(reduce +)))))] (reduce +)))))]
(f 0 0 0))) (f 0 0 0)))
;; part 1 ;; part 1