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

View File

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