diff --git a/clojure/src/question_3.clj b/clojure/src/question_3.clj index 127cab6..22e7f72 100644 --- a/clojure/src/question_3.clj +++ b/clojure/src/question_3.clj @@ -9,19 +9,44 @@ lines (strings/split-lines input)] ;(mapv Integer/parseInt))] (mapv parse-line lines))) -(defn all-possible-joltages [line] +(defn all-possible-two-joltages [line] (let [n (count line)] (for [s (range n) e (range (inc s) n)] - ;[s e] (+ (* 10 (get line s)) (get line e))))) - -(defn line-joltage [line] - (apply max (all-possible-joltages line))) +(defn line-two-joltage [line] + (apply max (all-possible-two-joltages line))) (defn question-3-a [document] - (reduce + (map line-joltage (parse-input document)))) + (reduce + (map line-two-joltage (parse-input document)))) + +(defn first-max-with-index [lst] + (let [max-value (volatile! -1) + max-index (volatile! -1)] + (when (seq lst) + (loop [[h & rest] lst + idx 0] + (when (> h @max-value) + (vreset! max-value h) + (vreset! max-index idx)) + (when (seq rest) + (recur rest (inc idx))))) + [@max-index @max-value])) + +(defn pow [x n] + (reduce *' (repeat n x))) + +(defn line-n-joltage [line n] + (if (zero? n) + 0 + (let [[idx val] (first-max-with-index (drop-last (dec n) line))] + (+ (* (pow 10 (dec n)) val) + (line-n-joltage (drop (inc idx) line) (dec n)))))) + +(defn question-3-b [document] + (reduce + (map #(line-n-joltage % 12) (parse-input document)))) (defn -main [& args] - (println "Solution to question 3-a is: " (question-3-a inputs/question-3))) \ No newline at end of file + (println "Solution to question 3-a is: " (question-3-a inputs/question-3)) + (println "Solution to question 3-b is: " (question-3-b inputs/question-3))) \ No newline at end of file