From e15e01a9a5e468155436a639d6823a36ed11758e Mon Sep 17 00:00:00 2001 From: Aleh Suprunovich Date: Sat, 6 Dec 2025 11:58:41 +0300 Subject: [PATCH] day 6 --- src/day6.clj | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/day6.clj diff --git a/src/day6.clj b/src/day6.clj new file mode 100644 index 0000000..e1f7f6a --- /dev/null +++ b/src/day6.clj @@ -0,0 +1,53 @@ +(ns day6 + (:require [clojure.edn :as edn] + [clojure.string :as string] + [helpers :refer [get-input]])) + +(def example +"123 328 51 64 + 45 64 387 23 + 6 98 215 314 +* + * + ") + +;;(def input (string/split-lines example)) +(def input (string/split-lines (get-input 6))) + +(defn calc [& args] + (let [v (mapv edn/read-string args) + op (peek v) + ns (pop v)] + (apply (resolve op) ns))) + +(println "Total sum:" + (->> + input + (mapv #(string/split (string/trim %) #"\ +")) + (apply map calc) + (reduce +))) + +(def nums (mapv vec (pop input))) +(def ops (mapv (comp resolve edn/read-string) + (string/split (string/trim (peek input)) #"\ +"))) + +(defn get-ns [lines] + (loop [ns [] + lines lines] + (if (empty? (first lines)) + [ns []] + (let [n (->> (mapv peek lines) + (apply str) + string/trim)] + (if (zero? (count n)) + [ns (mapv pop lines)] + (recur (conj ns (edn/read-string n)) + (mapv pop lines))))))) + +(println "Result (part 2):" + (loop + [lines nums + ops ops + rs []] + (if (empty? ops) + (reduce + rs) + (let [[ns lines] (get-ns lines)] + (recur lines (pop ops) (conj rs (apply (peek ops) ns)))))))