This commit is contained in:
Aleh Suprunovich 2025-12-03 16:51:34 +03:00
parent f91fe68f6b
commit 8b076d117d

76
src/day3.clj Normal file
View File

@ -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 +)))