From 8b076d117d8191061faabbdf43569bb1ad8d0ea6 Mon Sep 17 00:00:00 2001 From: Aleh Suprunovich Date: Wed, 3 Dec 2025 16:51:34 +0300 Subject: [PATCH] day 3 --- src/day3.clj | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/day3.clj diff --git a/src/day3.clj b/src/day3.clj new file mode 100644 index 0000000..24f4f8a --- /dev/null +++ b/src/day3.clj @@ -0,0 +1,76 @@ +(ns day3 + (:require [clojure.edn :as edn] + [clojure.string :as string] + [helpers :refer [get-input]])) + + +(def test-input "987654321111111 +811111111111119 +234234234234278 +818181911112111 +") + +(defn- max-joltage [bank] + (let [bank (->> bank string/trim (map str)) + joltages + (loop [r [] bank bank] + (if (= 1 (count bank)) + r + (recur + (into r (mapv (partial str (first bank)) (rest bank))) + (rest bank))))] + (->> (sort joltages) + last + Integer/parseInt))) + +;;(def input (string/split-lines test-input)) +(def input (string/split-lines (get-input 3))) + +(println "Total output joltage:" + (->> + input + (map max-joltage) + (reduce +))) + +(defn s->bats [s] + (map (comp Integer/parseInt str) s)) + +(defn- max-with-tail + [bank n] + (loop [r [[(first bank) (rest bank)]] + bank (rest bank)] + (if (> n (count bank)) + r + (let [c (first bank) + m (-> r first first) + bank (rest bank)] + (cond + (= c m) + (recur (conj r [c bank]) bank) + (> c m) + (recur [[c bank]] bank) + (< c m) + (recur r bank)))))) + +(defn- len [n] + (-> n str count)) + +(defn- next-j [[j bank]] + (let [r (max-with-tail bank (- 12 (len j)))] + (mapv #(vector (+ (* 10 j) (first %)) (second %)) r))) + +(defn- max-joltage-12 [bank] + (let [bank (map (comp Integer/parseInt str) bank)] + (->> + (loop [r (max-with-tail bank 12)] + (if (= 12 (-> r first first len)) + r + (recur (mapcat next-j r)))) + (map first) + (apply max)))) + +(println + "Total output joltage for 12 batteries:" + (->> + (map max-joltage-12 input) + (reduce +)))