Skip to content

Commit

Permalink
even more styling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed May 14, 2024
1 parent 92abe83 commit 55854a4
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 123 deletions.
9 changes: 4 additions & 5 deletions clojure/day05.clj
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@


(defn part-1 [seeds maps]
(->> (reduce convert-1 seeds maps)
(reduce min)))
(reduce min (reduce convert-1 seeds maps)))

(defn part-2 [seeds maps]
(->> (reduce convert-2 seeds maps)
Expand All @@ -76,9 +75,9 @@

(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)]
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)]))

Expand Down
14 changes: 7 additions & 7 deletions clojure/day07.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@


(defn parse-hand [jokers? [cards bid]]
(let [cards' (change-cards-representation cards jokers?)
jokers (aoc/count-if #{\0} cards')
(let [cards' (change-cards-representation cards jokers?)
jokers (aoc/count-if #{\0} cards')
card-counts (->> (str/replace cards' "0" "")
frequencies
vals
(sort >)
vec)
hand-type (match card-counts
[] [jokers 0]
[x] [(+ jokers x) 0]
[x y & _] [(+ jokers x) y])]
hand-type (match card-counts
[] [jokers 0]
[x] [(+ jokers x) 0]
[x y & _] [(+ jokers x) y])]
[hand-type cards' (parse-long bid)]))


Expand All @@ -38,7 +38,7 @@

(defn total-winnings [hands jokers?]
(->> hands
(map (partial parse-hand jokers?))
(map #(parse-hand jokers? %))
sort
(into [[0 "" 0]]) ; to have ranks start at 1
calc-score))
Expand Down
6 changes: 3 additions & 3 deletions clojure/day08.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@

(defn ghost-steps [instrs network starts]
(transduce
(map (partial steps instrs network))
(map #(steps instrs network %))
aoc/lcm
starts))


(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))]
instrs (mapv (comp keyword str) instrs')
starts (filter #(str/ends-with? % "A") (keys network))]
[(steps instrs network :AAA)
(ghost-steps instrs network starts)]))

Expand Down
18 changes: 9 additions & 9 deletions clojure/day10.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

(defn traverse [sketch start]
(loop [[x y :as curr] (aoc/pt+ start [0 1])
[px py] start
seen (transient #{})
[px py] start
seen (transient #{})
verticals (transient {})]
(let [seen' (conj! seen curr)
dx (- x px)
Expand All @@ -32,22 +32,22 @@
curr seen' verticals)))))

(defn enclosed [seen verticals h w]
(for [y (range h)
:let [row-verts (verticals y)]
(for [y (range h)
:let [row-verts (verticals y)]
:when row-verts
:let [min-vert (reduce min row-verts)
:let [min-vert (reduce min row-verts)
max-vert (reduce max row-verts)]
x (range w)
x (range w)
:when (and (< min-vert x max-vert)
(not (seen [x y]))
(odd? (aoc/count-if #(< % x) row-verts)))]
1))

(defn solve [input]
(let [sketch (aoc/parse-input input :chars)
start (find-start sketch)
h (count sketch)
w (count (first sketch))
start (find-start sketch)
h (count sketch)
w (count (first sketch))
{:keys [pipes verts]} (traverse sketch start)]
[(/ (count pipes) 2)
(count (enclosed pipes verts h w))]))
Expand Down
8 changes: 4 additions & 4 deletions clojure/day11.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

(defn find-galaxies [lines]
(for [[y line] (map-indexed vector lines)
[x chr] (map-indexed vector line)
:when (= chr \#)]
[x chr] (map-indexed vector line)
:when (= chr \#)]
[x y]))


Expand All @@ -25,7 +25,7 @@
(-> acc
(update :sum + (* coeff coord))
(update :coeff + 2)))
{:sum 0
{:sum 0
:coeff (- 1 (count galaxies))})
:sum))

Expand All @@ -35,7 +35,7 @@
(reduce
(fn [acc coord]
(let [before (count (take-while #(< % coord) galaxies))
after (- total before)]
after (- total before)]
(+ acc (* before after))))
0
empties)))
Expand Down
14 changes: 7 additions & 7 deletions clojure/day12.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
(if (empty? groups)
(if (every? is-operational? pattern) 1 0)
(let [[size & tl] groups
post (+ (reduce + tl) (count tl))
score (atom 0)]
(doseq [pre (range (inc (- (count pattern) post size)))
:let [[before pattern'] (split-at pre pattern)
[current remaining] (split-at size pattern')]
post (+ (reduce + tl) (count tl))
score (atom 0)]
(doseq [pre (range (inc (- (count pattern) post size)))
:let [[before pattern'] (split-at pre pattern)
[current remaining] (split-at size pattern')]
:while (every? is-operational? before)
:when (every? is-damaged? current)]
:when (every? is-damaged? current)]
(cond
(empty? tl) (when (every? is-operational? remaining)
(swap! score inc))
Expand All @@ -45,7 +45,7 @@


(defn solve [input]
(let [lines (aoc/parse-input input parse-line)
(let [lines (aoc/parse-input input parse-line)
unfolded (map unfold lines)]
[(reduce + (map arrangements lines))
(reduce + (pmap arrangements unfolded))]))
Expand Down
8 changes: 4 additions & 4 deletions clojure/day13.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

(defn mirror? [part pattern nrettap line]
(let [before (take-last line nrettap)
after (drop line pattern)
diffs (map differences before after)]
after (drop line pattern)
diffs (map differences before after)]
(case part
1 (every? zero? diffs)
2 (= 1 (reduce + diffs)))))


(defn mirror-line [part pattern]
(aoc/find-first
(partial mirror? part pattern (rseq pattern))
#(mirror? part pattern (rseq pattern) %)
(range 1 (count pattern))))


Expand All @@ -28,7 +28,7 @@


(defn notes-sum [patterns part]
(reduce + (pmap (partial find-mirror part) patterns)))
(reduce + (pmap #(find-mirror part %) patterns)))


(defn solve [input]
Expand Down
12 changes: 6 additions & 6 deletions clojure/day14.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
(->> (str/split line #"#" -1)
(map (comp str/join
(case dir
:left reverse
:left reverse
:right identity)
sort))
(str/join "#")))


(defn move-east [platform]
(pmap (partial move-line :right) platform))
(pmap #(move-line :right %) platform))

(defn move-west [platform]
(pmap (partial move-line :left) platform))
(pmap #(move-line :left %) platform))

(defn move-north [platform]
(-> platform rotate move-west rotate))
Expand All @@ -37,7 +37,7 @@

(defn calc-score [platform]
(let [platform (vec platform)
size (count platform)]
size (count platform)]
(reduce
(fn [acc n]
(+ acc (* (- size n)
Expand All @@ -47,8 +47,8 @@


(defn remaining-shakes [platform prev curr]
(let [r (- 1000000000 prev)
to-spin (mod r (- curr prev))]
(let [remain (- 1000000000 prev)
to-spin (mod remain (- curr prev))]
(nth (iterate spin-cycle platform) to-spin)))


Expand Down
4 changes: 2 additions & 2 deletions clojure/day15.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@


(defn focusing-power [boxes]
(reduce + (for [[i box] (map-indexed vector boxes)
(reduce + (for [[i box] (map-indexed vector boxes)
[j [_ focal]] (map-indexed vector box)]
(* (inc i) (inc j) focal))))


(defn solve [input]
(let [steps (aoc/parse-input-line input :words #",")
(let [steps (aoc/parse-input-line input :words #",")
instructions (map parse-instruction steps)]
[(reduce + (map word-hash steps))
(focusing-power (hashmap instructions))]))
Expand Down
50 changes: 24 additions & 26 deletions clojure/day17.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,34 @@


(defn traverse [city min-straight max-straight]
(let [size (count city)
end (dec size)
(let [size (count city)
end (dec size)
queue (priority-map [0 0 1 0] 0
[0 0 0 1] 0)]
(loop [seen #{}
(loop [seen #{}
queue queue]
(let [[[x y dx dy :as state] heat] (peek queue)
queue' (pop queue)]
(if (= end x y)
heat
(if (seen state)
(recur seen queue')
(recur
(conj seen state)
(reduce
(fn [q [dx' dy']]
(reduce
(fn [q n]
(let [nx (+ x (* n dx'))
ny (+ y (* n dy'))]
(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']]
(assoc q state' (min heat' (q state' Integer/MAX_VALUE)))))))
q
(range min-straight (inc max-straight))))
queue'
[[(- dy) dx]
[dy (- dx)]]))))))))
queue' (pop queue)]
(cond
(= end x y) heat
(seen state) (recur seen queue')
:else (recur
(conj seen state)
(reduce
(fn [q [dx' dy']]
(reduce (fn [q n]
(let [nx (+ x (* n dx'))
ny (+ y (* n dy'))]
(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']]
(assoc q state' (min heat' (q state' Integer/MAX_VALUE)))))))
q
(range min-straight (inc max-straight))))
queue'
[[(- dy) dx]
[dy (- dx)]])))))))


(defn solve [input]
Expand Down
16 changes: 8 additions & 8 deletions clojure/day18.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@


(defn parse-line [line]
(let [[d n c] (str/split line #" ")
c (str/replace c #"[(#)]" "")
(let [[d n c] (str/split line #" ")
c (str/replace c #"[(#)]" "")
[dist dir] (map str/join (split-at 5 c))
dist (Integer/parseInt dist 16)]
dist (Integer/parseInt dist 16)]
{:p1 {:dir (deltas d)
:dist (parse-long n)}
:p2 {:dir (deltas dir)
:dist dist}}))


(defn dig-trench [input]
; Shoelace formula + Pick's theorem
;; Shoelace formula + Pick's theorem
(->> input
(reduce (fn [{:keys [total x]}
{:keys [dir dist]}]
(let [[dx dy] dir
nx (+ x (* dist dx))
area (* nx dist dy)]
{:x nx
nx (+ x (* dist dx))
area (* nx dist dy)]
{:x nx
:total (+ total area (/ dist 2))}))
{:total 1
:x 0})
:x 0})
:total
long))

Expand Down
2 changes: 1 addition & 1 deletion clojure/day19.clj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
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))
[(reduce + (map #(accepted workflows %) ratings))
(accepted-combinations workflows rating-2)]))


Expand Down
Loading

0 comments on commit 55854a4

Please sign in to comment.