Skip to content

Commit

Permalink
aoc.clj: separate file reading from parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed May 5, 2024
1 parent 0e012fb commit 2044541
Show file tree
Hide file tree
Showing 23 changed files with 104 additions and 100 deletions.
58 changes: 30 additions & 28 deletions clojure/aoc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
(def empty-queue clojure.lang.PersistentQueue/EMPTY)


(defn read-file [file]
(let [name (if (int? file)
(format "%02d" file)
file)]
(slurp (str "inputs/" name ".txt"))))


(defn integers
[s & {:keys [negative?]
:or {negative? true}}]
Expand All @@ -16,40 +23,29 @@
(map parse-long)
(filterv some?)))


(defn parse-multiline-string
(defn parse-input
[s & [parse-fn {:keys [word-sep nl-sep]}]]
(->> (str/split s (or nl-sep #"\n"))
((if (nil? parse-fn) identity
(partial mapv
(case parse-fn
:int parse-long
:ints integers
:digits string->digits
:chars vec
:words #(str/split % (or word-sep #" "))
parse-fn))))))


(defn read-input
[input & [parse-fn seps]]
(let [name (if (int? input)
(format "%02d" input)
input)]
(-> (str "inputs/" name ".txt")
slurp
(parse-multiline-string parse-fn seps))))

(defn read-input-line
(let [f (case parse-fn
:int parse-long
:ints integers
:digits string->digits
:chars vec
:words #(str/split % (or word-sep #" "))
nil identity
parse-fn)]
(mapv f (str/split s (or nl-sep #"\n")))))

(defn parse-input-line
[input & [parse-fn word-sep]]
(-> input
(read-input parse-fn {:word-sep word-sep})
(parse-input parse-fn {:word-sep word-sep})
first))

(defn read-input-paragraphs
(defn parse-input-paragraphs
[input & [parse-fn word-sep]]
(->> (read-input input nil {:nl-sep #"\n\n"})
(mapv #(parse-multiline-string % parse-fn {:word-sep word-sep}))))
(-> input
(parse-input nil {:nl-sep #"\n\n"})
(->> (mapv #(parse-input % parse-fn {:word-sep word-sep})))))


(defn transpose [matrix]
Expand All @@ -70,6 +66,12 @@
(defn pt+ ^longs [[^long ax ^long ay] [^long bx ^long by]]
[(+ ax bx) (+ ay by)])

(defn inside?
([size x y] (inside? size size x y))
([size-x size-y x y]
(and (< -1 x size-x)
(< -1 y size-y))))

(defn neighbours ^longs [[^long x ^long y] ^long amount]
(for [^long dy [-1 0 1]
^long dx [-1 0 1]
Expand Down
6 changes: 3 additions & 3 deletions clojure/day01.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
(reduce +)))


(defn solve [input-file]
(let [lines (aoc/read-input input-file)]
(defn solve [input]
(let [lines (aoc/parse-input input)]
[(calibration-sum lines #"\d" #"\d")
(calibration-sum lines first-patt last-patt)]))


(solve 1)
(solve (aoc/read-file 1))
6 changes: 3 additions & 3 deletions clojure/day02.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
(reduce * (vals game)))


(defn solve [input-file]
(let [lines (aoc/read-input input-file :words {:word-sep #": |; |, "})
(defn solve [input]
(let [lines (aoc/parse-input input :words {:word-sep #": |; |, "})
games (map maximums lines)]
[(reduce + (find-valid games))
(reduce + (map power games))]))


(solve 2)
(solve (aoc/read-file 2))
9 changes: 4 additions & 5 deletions clojure/day03.clj
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@
:gear-ratios (reduce + (gear-ratios (vals gear-map)))})))


(defn solve [input-file]
(let [lines (aoc/read-input input-file)
(defn solve [input]
(let [lines (aoc/parse-input input)
points (aoc/grid->points lines #(not= % \.))
symbs (symbol-coords points)
gears (set (filter #(= \* (points %)) symbs))
solution (find-solution lines symbs gears)]
[(:num-sum solution)
(:gear-ratios solution)]))
((juxt :num-sum :gear-ratios) solution)))


(solve 3)
(solve (aoc/read-file 3))
6 changes: 3 additions & 3 deletions clojure/day04.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
(range size))))


(defn solve [input-file]
(let [lines (aoc/read-input input-file parse-line)
(defn solve [input]
(let [lines (aoc/parse-input input parse-line)
winners (mapv winning-numbers lines)]
[(reduce + (map points winners))
(reduce + (new-rules winners))]))


(solve 4)
(solve (aoc/read-file 4))
6 changes: 3 additions & 3 deletions clojure/day05.clj
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@
:start))


(defn solve [input-file]
(let [[[seeds] & maps] (aoc/read-input-paragraphs input-file)
(defn solve [input]
(let [[[seeds] & maps] (aoc/parse-input-paragraphs input)
seeds-1 (aoc/integers seeds)
seeds-2 (seed-ranges seeds-1)
rules (map parse-maps maps)]
[(part-1 seeds-1 rules)
(part-2 seeds-2 rules)]))


(solve 5)
(solve (aoc/read-file 5))
6 changes: 3 additions & 3 deletions clojure/day06.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
(inc (- (- time x1) x1))))


(defn solve [input-file]
(let [[times distances] (aoc/read-input input-file :ints)
(defn solve [input]
(let [[times distances] (aoc/parse-input input :ints)
time-2 (fix-keming times)
distance-2 (fix-keming distances)]
[(transduce (map find-winners) * (zipmap times distances))
(find-winners [time-2 distance-2])]))


(solve 6)
(solve (aoc/read-file 6))
6 changes: 3 additions & 3 deletions clojure/day07.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
calc-score))


(defn solve [input-file]
(let [hands (aoc/read-input input-file :words)]
(defn solve [input]
(let [hands (aoc/parse-input input :words)]
[(total-winnings hands false)
(total-winnings hands true)]))


(solve 7)
(solve (aoc/read-file 7))
6 changes: 3 additions & 3 deletions clojure/day08.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
starts))


(defn solve [input-file]
(let [[[instrs'] network'] (aoc/read-input-paragraphs input-file)
(defn solve [input]
(let [[[instrs'] network'] (aoc/parse-input-paragraphs input)
network (parse-network network')
instrs (mapv (comp keyword str) instrs')
starts (filter #(str/ends-with? % "A") (keys network))]
[(steps instrs network :AAA)
(ghost-steps instrs network starts)]))


(solve 8)
(solve (aoc/read-file 8))
6 changes: 3 additions & 3 deletions clojure/day09.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
(reduce +)))


(defn solve [input-file]
(let [histories (aoc/read-input input-file :ints)]
(defn solve [input]
(let [histories (aoc/parse-input input :ints)]
[(prev-sum rseq histories)
(prev-sum identity histories)]))


(solve 9)
(solve (aoc/read-file 9))
6 changes: 3 additions & 3 deletions clojure/day10.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
(odd? (aoc/count-if #(< % x) row-verts)))]
1))

(defn solve [input-file]
(let [sketch (aoc/read-input input-file :chars)
(defn solve [input]
(let [sketch (aoc/parse-input input :chars)
start (find-start sketch)
h (count sketch)
w (count (first sketch))
Expand All @@ -50,4 +50,4 @@
(count (enclosed pipes verts h w))]))


(solve 10)
(solve (aoc/read-file 10))
6 changes: 3 additions & 3 deletions clojure/day11.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
empties)))


(defn solve [input-file]
(let [lines (aoc/read-input input-file)
(defn solve [input]
(let [lines (aoc/parse-input input)
galaxies (find-galaxies lines)
galaxies-x (sort (map first galaxies))
galaxies-y (sort (map second galaxies))
Expand All @@ -56,4 +56,4 @@
(+ distances (* multi expansions))]))


(solve 11)
(solve (aoc/read-file 11))
6 changes: 3 additions & 3 deletions clojure/day12.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
@score))))


(defn solve [input-file]
(let [lines (aoc/read-input input-file parse-line)
(defn solve [input]
(let [lines (aoc/parse-input input parse-line)
unfolded (map unfold lines)]
[(reduce + (map arrangements lines))
(reduce + (pmap arrangements unfolded))]))


(solve 12)
(solve (aoc/read-file 12))
6 changes: 3 additions & 3 deletions clojure/day13.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
(reduce + (pmap (partial find-mirror part) patterns)))


(defn solve [input-file]
(let [patterns (aoc/read-input-paragraphs input-file)]
(defn solve [input]
(let [patterns (aoc/parse-input-paragraphs input)]
[(notes-sum patterns 1)
(notes-sum patterns 2)]))


(solve 13)
(solve (aoc/read-file 13))
6 changes: 3 additions & 3 deletions clojure/day14.clj
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
(range)))


(defn solve [input-file]
(let [platform (aoc/read-input input-file)]
(defn solve [input]
(let [platform (aoc/parse-input input)]
[(calc-score (move-north platform))
(calc-score (shake platform))]))


(solve 14)
(solve (aoc/read-file 14))
6 changes: 3 additions & 3 deletions clojure/day15.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
(* (inc i) (inc j) focal))))


(defn solve [input-file]
(let [steps (aoc/read-input-line input-file :words #",")
(defn solve [input]
(let [steps (aoc/parse-input-line input :words #",")
instructions (map parse-instruction steps)]
[(reduce + (map word-hash steps))
(focusing-power (hashmap instructions))]))


(solve 15)
(solve (aoc/read-file 15))
6 changes: 3 additions & 3 deletions clojure/day16.clj
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
(reduce max))))


(defn solve [input-file]
(let [contraption (aoc/read-input input-file :chars)]
(defn solve [input]
(let [contraption (aoc/parse-input input :chars)]
[(traverse contraption 0 0 1 0)
(max-energy contraption)]))


(solve 16)
(solve (aoc/read-file 16))
11 changes: 4 additions & 7 deletions clojure/day17.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
(defn traverse [city min-straight max-straight]
(let [size (count city)
end (dec size)
valid? (fn [x y] (and (< -1 x size)
(< -1 y size)))
queue (priority-map [0 0 1 0] 0
[0 0 0 1] 0)]
(loop [seen #{}
Expand All @@ -33,7 +31,7 @@
(fn [q n]
(let [nx (+ x (* n dx'))
ny (+ y (* n dy'))]
(if-not (valid? nx ny)
(if-not (aoc/inside? size nx ny)
(reduced q)
(let [heat' (+ heat (heat-loss city x y dx' dy' n))
state' [nx ny dx' dy']]
Expand All @@ -45,11 +43,10 @@
[dy (- dx)]]))))))))


(defn solve [input-file]
(let [city (aoc/read-input input-file :digits)]
(defn solve [input]
(let [city (aoc/parse-input input :digits)]
[(traverse city 1 3)
(traverse city 4 10)]))


(solve 17)

(solve (aoc/read-file 17))
6 changes: 3 additions & 3 deletions clojure/day18.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
long))


(defn solve [input-file]
(let [dig-plan (aoc/read-input input-file parse-line)]
(defn solve [input]
(let [dig-plan (aoc/parse-input input parse-line)]
[(dig-trench (map :p1 dig-plan))
(dig-trench (map :p2 dig-plan))]))


(solve 18)
(solve (aoc/read-file 18))
6 changes: 3 additions & 3 deletions clojure/day19.clj
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@



(defn solve [input-file]
(let [[wrkfls rtngs] (aoc/read-input-paragraphs input-file)
(defn solve [input]
(let [[wrkfls rtngs] (aoc/parse-input-paragraphs input)
workflows (into {} (map parse-workflow wrkfls))
ratings (map parse-rating rtngs)
rating-2 (zipmap [:x :m :a :s] (repeat [1 4000]))]
[(reduce + (map (partial accepted workflows) ratings))
(accepted-combinations workflows rating-2)]))


(solve 19)
(solve (aoc/read-file 19))
Loading

0 comments on commit 2044541

Please sign in to comment.