From 872bc909ff74ce3e938467360c22a4734a4b42a9 Mon Sep 17 00:00:00 2001 From: Adam Jeniski Date: Sun, 17 Dec 2023 10:19:28 -0500 Subject: [PATCH] ajet.core coming soon to a repository near you?! --- 2023/src/core.clj | 31 +++++++++++++++++-------- 2023/src/day12.clj | 57 +++++++++++++++++++++++----------------------- 2 files changed, 50 insertions(+), 38 deletions(-) diff --git a/2023/src/core.clj b/2023/src/core.clj index 3637f8c..588d6c3 100644 --- a/2023/src/core.clj +++ b/2023/src/core.clj @@ -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) + ; + ) diff --git a/2023/src/day12.clj b/2023/src/day12.clj index debba71..f373c33 100644 --- a/2023/src/day12.clj +++ b/2023/src/day12.clj @@ -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