Skip to content

Commit

Permalink
2024 style refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Jun 6, 2024
1 parent 908b07a commit 4bcadb4
Show file tree
Hide file tree
Showing 27 changed files with 147 additions and 145 deletions.
107 changes: 62 additions & 45 deletions clojure/aoc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,49 @@
(:require [clojure.string :as str]))


(defn parse-multiline-string
[s & [out-type sep]]
(->> (str/split s (or sep #"\n"))
((case out-type
:int (partial map parse-long)
:list (partial map #(str/split % #""))
:vector (partial mapv vec)
nil identity))))


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


(defn read-input-line
[input & [sep]]
(->> input
read-input
first
((case sep
nil identity
(partial #(str/split % sep))))))

(defn read-input-paragraphs
[input & [out-type]]
(->> (read-input input nil #"\n\n")
(mapv #(parse-multiline-string % out-type))))
(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}}]
(mapv parse-long
(re-seq (if negative? #"-?\d+" #"\d+") s)))

(defn string->digits [s]
(->> (str/split s #"")
(mapv parse-long)))
(map parse-long)
(filterv some?)))

(defn parse-input
[s & [parse-fn {:keys [word-sep nl-sep]}]]
(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
(parse-input parse-fn {:word-sep word-sep})
first))

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


(defn transpose [matrix]
Expand All @@ -57,17 +62,16 @@
(int (first s)))


(defn integers
[s & {:keys [negative?]
:or {negative? true}}]
(mapv parse-long
(re-seq (if negative? #"-?\d+" #"\d+") s)))


(defn pt+ ^longs [[^long ax ^long ay] [^long bx ^long by]]
[(+ ax bx) (+ ay by)])

(defn neighbours ^longs [[^long x ^long y] ^long amount]
(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 amount [^long x ^long y]]
(for [^long dy [-1 0 1]
^long dx [-1 0 1]
:when
Expand All @@ -84,15 +88,16 @@
[x y (dec z)] [x y (inc z)]])


(defn vec2d->grid
([v] (vec2d->grid v identity))
(defn grid->points
([v] (grid->points v identity))
([v pred]
(into {}
(for [[y line] (map-indexed vector v)
[x char] (map-indexed vector line)
:when (pred char)]
[[x y] char]))))


(defn none? [pred xs]
;; Faster version of `not-any?`.
(reduce
Expand All @@ -113,7 +118,7 @@
false
(recur (dec idx) acc)))))

(defn count-if [pred xs]
(defn count-if ^long [pred xs]
(reduce
(fn [^long acc x]
(if (pred x) (inc acc) acc))
Expand All @@ -126,3 +131,15 @@
(when (pred x) (reduced x)))
nil
xs))


(defn gcd
([] 1)
([x] x)
([a b] (if (zero? b) a
(recur b (mod a b)))))

(defn lcm
([] 1)
([x] x)
([a b] (/ (* a b) (gcd a b))))
4 changes: 2 additions & 2 deletions clojure/day01.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@


(defn parse-input [input]
(->> (aoc/read-input-paragraphs input :int)
(->> (aoc/parse-input-paragraphs input :int)
(map #(reduce + %))
(sort >)))

(defn solve
([] (solve 1))
([] (solve (aoc/read-file 1)))
([input]
(let [calories (parse-input input)]
[(first calories)
Expand Down
5 changes: 2 additions & 3 deletions clojure/day02.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@
(transduce (map rules) + guide))

(defn solve
([] (solve 2))
([] (solve (aoc/read-file 2)))
([input]
(let [strategy-guide (->> (aoc/read-input input)
(mapv #(take-nth 2 %)))]
(let [strategy-guide (aoc/parse-input input #(take-nth 2 %))]
[(play p1-rules strategy-guide)
(play p2-rules strategy-guide)])))

Expand Down
4 changes: 2 additions & 2 deletions clojure/day03.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
(def f2 #(partition 3 %))

(defn solve
([] (solve 3))
([] (solve (aoc/read-file 3)))
([input]
(let [rucksacks (aoc/read-input input)]
(let [rucksacks (aoc/parse-input input)]
[(priority-sum rucksacks f1)
(priority-sum rucksacks f2)])))

Expand Down
5 changes: 2 additions & 3 deletions clojure/day04.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
(<= a c b)))

(defn parse-input [input]
(->> (aoc/read-input input)
(mapv #(aoc/integers % {:negative? false}))))
(aoc/parse-input input #(aoc/integers % {:negative? false})))

(defn solve
([] (solve 4))
([] (solve (aoc/read-file 4)))
([input]
(let [assignments-pairs (parse-input input)]
[(aoc/count-if fully-contain? assignments-pairs)
Expand Down
12 changes: 6 additions & 6 deletions clojure/day05.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@
(remove empty?)
(into [[]]))) ; empty first to have real stacks start from index 1

(defn move-boxes [stacks [amount from to] & [pick-multiple?]]
(defn move-boxes [stacks [amount from to] pick-multiple?]
(let [[took remains] (split-at amount (stacks from))
put (into (stacks to) (if pick-multiple? (reverse took) took))]
(-> stacks
(assoc! from remains)
(assoc! to put))))

(defn operate-crane [stacks instructions & pick-multiple?]
(defn operate-crane [stacks instructions pick-multiple?]
(->> instructions
(reduce #(move-boxes %1 %2 pick-multiple?) (transient stacks))
persistent!
(map first)
(apply str)))

(defn solve
([] (solve 5))
([] (solve (aoc/read-file 5)))
([input]
(let [[raw-stacks raw-instructions] (aoc/read-input-paragraphs input)
(let [[raw-stacks raw-instructions] (aoc/parse-input-paragraphs input)
stacks (parse-stacks raw-stacks)
instructions (mapv aoc/integers raw-instructions)
operate (partial operate-crane stacks instructions)]
[(operate)
operate #(operate-crane stacks instructions %)]
[(operate false)
(operate :CrateMover9001)])))


Expand Down
4 changes: 2 additions & 2 deletions clojure/day06.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
(iterate inc length)))

(defn solve
([] (solve 6))
([] (solve (aoc/read-file 6)))
([input]
(let [datastream-buffer (aoc/read-input-line input)]
(let [datastream-buffer (aoc/parse-input-line input)]
[(process datastream-buffer 4)
(process datastream-buffer 14)])))

Expand Down
6 changes: 3 additions & 3 deletions clojure/day07.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
(:or ["$" "ls"]
["dir" _]) [sizes path]
[size _] [(add! path size sizes) path]))
[(transient {}) []])
[(transient {}) '()])
first
persistent!))

Expand All @@ -38,9 +38,9 @@
(reduce min))))

(defn solve
([] (solve 7))
([] (solve (aoc/read-file 7)))
([input]
(let [folders (calc-sizes (aoc/read-input input))
(let [folders (calc-sizes (aoc/parse-input input))
total-size (folders ["/"])
sizes (vals folders)]
[(part-1 sizes)
Expand Down
7 changes: 3 additions & 4 deletions clojure/day08.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@
:scenic-score (transduce (map #(viewing-distance height %)) * dirs)})))

(defn solve
([] (solve 8))
([] (solve (aoc/read-file 8)))
([input]
(let [height-map (->> (aoc/read-input input)
(mapv aoc/string->digits))
results (go-through-forrest height-map)]
(let [height-map (aoc/parse-input input :digits)
results (go-through-forrest height-map)]
[(->> results (aoc/count-if :is-visible?))
(->> results (map :scenic-score) (reduce max))])))

Expand Down
4 changes: 2 additions & 2 deletions clojure/day09.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
motions))

(defn solve
([] (solve 9))
([] (solve (aoc/read-file 9)))
([input]
(let [motions (mapcat parse-motion (aoc/read-input input))
(let [motions (mapcat parse-motion (aoc/parse-input input))
[_ p1 p2] (simulate motions)]
[(count p1)
(count p2)])))
Expand Down
6 changes: 3 additions & 3 deletions clojure/day10.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
(map str/join)))

(defn solve
([] (solve 10))
([] (solve (aoc/read-file 10)))
([input]
(let [x-positions
(->> input
aoc/read-input
aoc/parse-input
read-instructions
(reductions + 1))] ; x starts at 1
[(part-1 x-positions)
(part-2 x-positions)])))


(solve 10)
(solve)
4 changes: 2 additions & 2 deletions clojure/day11.clj
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@

(defn parse-input [input]
(->> input
aoc/read-input-paragraphs
aoc/parse-input-paragraphs
(mapv parse-monkey)))

(defn solve
([] (solve 11))
([] (solve (aoc/read-file 11)))
([input]
(let [monkeys (parse-input input)]
[(play-game monkeys 20 true)
Expand Down
4 changes: 2 additions & 2 deletions clojure/day12.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
(- (int char) (int \a))])))

(defn solve
([] (solve 12))
([] (solve (aoc/read-file 12)))
([input]
(let [grid (make-grid (aoc/read-input input :vector))
(let [grid (make-grid (aoc/parse-input input :chars))
p1 (future (travel grid 1))
p2 (future (travel grid 2))]
[@p1 @p2])))
Expand Down
4 changes: 2 additions & 2 deletions clojure/day13.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@

(defn parse-input [input]
(->> input
aoc/read-input
aoc/parse-input
(remove empty?)
(mapv read-string)
(partition 2)))

(defn solve
([] (solve 13))
([] (solve (aoc/read-file 13)))
([input]
(let [packets (parse-input input)]
[(part-1 packets)
Expand Down
7 changes: 4 additions & 3 deletions clojure/day14.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@

(defn add-floor [rocks]
(let [floor-y (+ 2 (reduce max (map #(mod % X) rocks)))
floor (for [x (range (- start-x floor-y) (+ start-x floor-y 1))] (+ (* X x) floor-y ))]
floor (for [x (range (- start-x floor-y) (+ start-x floor-y 1))]
(+ (* X x) floor-y))]
(into rocks floor)))

(defn parse-input [input]
(->> input
aoc/read-input
aoc/parse-input
(into (dense-int-set)
(comp
(map aoc/integers)
Expand All @@ -67,7 +68,7 @@
add-floor))

(defn solve
([] (solve 14))
([] (solve (aoc/read-file 14)))
([input]
(let [rocks (parse-input input)]
[(part-1 rocks)
Expand Down
Loading

0 comments on commit 4bcadb4

Please sign in to comment.