Aoc

Advent of Code day 3

This day’s questions were a lot easier for me than yesterday’s, and i was able to write O(N) code, which really satisfied me. Without any further delay, lets get into the code!

(defn read-input [& [file]]
  (line-seq (io/reader (or file "data/dec3.txt"))))

(defn process-input [input]
  (let* [lst (map seq input)
         nums-lst (map (fn [lst] (map #(Character/digit % 10) lst)) lst)
         nums-vec (apply vector (map #(apply vector %) nums-lst))]
    nums-vec))

(defn get-max [v start end]
  (loop [x -1
         val -1
         i start]
    (cond
      (= end i) (list x val)
      (> (nth v i) val) (recur i (nth v i) (inc i))
      :else (recur x val (inc i)))))

(defn get-max-jolt-bank [bank]
  (let [[fi fd] (get-max bank 0 (dec (count bank)))
        [si sd] (get-max bank (inc fi) (count bank))]
    (+ (* 10 fd) sd)))

(defn solve []
  (let [banks (process-input (read-input))]
    (apply + (map get-max-jolt-bank banks))))

;; Part 2

(defn get-max-digit-jolt-bank-lst [bank digit]
  (loop [num '()
         start 0
         end (- (count bank) (dec digit))]
    (if (= (dec end) (count bank))
      num
      (let [[index n] (get-max bank start end)]
        (recur (conj num n) (inc index) (inc end))))))

(defn get-max-digit-jolt [bank digit]
  (loop [curr 0
         digits (get-max-digit-jolt-bank-lst bank digit)
         base 1]
    (if (empty? digits)
      curr
      (recur (+ curr (* (first digits) base)) (rest digits) (* base 10)))))

(defn solve-part2 []
  (let [input (process-input (read-input))]
    (apply + (map #(bigint (get-max-digit-jolt % 12)) input))))

Let’s discuss the algorithm for the second part, as you can understand first part’s algorithm from that as well.

Advent Of Code 2025: Clojure Edition!

It’s finally December, and this year my college decided to end exams early, so i can for the first time truly enjoy Advent of Code! I have decided to attempt it in clojure, as it is one of the lisps i have never used, and it is widely used in the industry compared to other lisps. I’d like to discuss the algorithms (which are horribly bad), and more importantly the code i came up with to implement the algorithm.