mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 09:23:17 -09:00
ajet.core coming soon to a repository near you?!
This commit is contained in:
parent
8d236e36ea
commit
872bc909ff
@ -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)
|
||||
;
|
||||
)
|
||||
|
@ -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,8 +10,7 @@
|
||||
(defn ans [dots blocks]
|
||||
(let
|
||||
[len-blocks (count blocks)
|
||||
f
|
||||
(mfn f [i bi current]
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user