AOC2022 Day1


AOC

Sample input for the Day 1 (input.txt)

1000
2000
3000

4000

5000
6000

7000
8000
9000

10000

Part 1

(defn part1 [file]
  
  (let [input (clojure.string/split-lines (slurp file))
        max-number (atom 0)]

  (loop [counter 0 
         elements input]
    (let [element (first elements)]
      (if (nil? element)
        @max-number
        (if (clojure.string/blank? element)
          (do (swap! max-number max counter)
              (recur 0 (rest elements)))
          (recur (+ counter (Integer/parseInt element)) (rest elements))))))))

(part1 "input.txt")

Part 2

(defn part2 [file]
  
  (let [input (clojure.string/split-lines (slurp file))
        max-number (atom [])]

  (loop [counter 0 
         elements input]
    (let [element (first elements)]
      (if (nil? element)
        @max-number
        (if (clojure.string/blank? element)
          (do (swap! max-number conj counter)
              (recur 0 (rest elements)))
          (recur (+ counter (Integer/parseInt element)) (rest elements))))))))

(apply + (take 3 (sort > (part2 "input.txt"))))

Learned from others

Reference