From 55854a4ba311e86762abcbebf687bfa06e3d3047 Mon Sep 17 00:00:00 2001 From: narimiran Date: Tue, 14 May 2024 14:32:21 +0200 Subject: [PATCH] even more styling changes --- clojure/day05.clj | 9 +++-- clojure/day07.clj | 14 ++++---- clojure/day08.clj | 6 ++-- clojure/day10.clj | 18 +++++----- clojure/day11.clj | 8 ++--- clojure/day12.clj | 14 ++++---- clojure/day13.clj | 8 ++--- clojure/day14.clj | 12 +++---- clojure/day15.clj | 4 +-- clojure/day17.clj | 50 +++++++++++++--------------- clojure/day18.clj | 16 ++++----- clojure/day19.clj | 2 +- clojure/day20.clj | 85 ++++++++++++++++++++++++----------------------- 13 files changed, 123 insertions(+), 123 deletions(-) diff --git a/clojure/day05.clj b/clojure/day05.clj index 6e62d0e..e6e23af 100644 --- a/clojure/day05.clj +++ b/clojure/day05.clj @@ -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) @@ -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)])) diff --git a/clojure/day07.clj b/clojure/day07.clj index 7f0d011..95cdfc9 100644 --- a/clojure/day07.clj +++ b/clojure/day07.clj @@ -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)])) @@ -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)) diff --git a/clojure/day08.clj b/clojure/day08.clj index 0bd8e6d..531c9b3 100644 --- a/clojure/day08.clj +++ b/clojure/day08.clj @@ -21,7 +21,7 @@ (defn ghost-steps [instrs network starts] (transduce - (map (partial steps instrs network)) + (map #(steps instrs network %)) aoc/lcm starts)) @@ -29,8 +29,8 @@ (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)])) diff --git a/clojure/day10.clj b/clojure/day10.clj index d02c5f2..e8ce5c2 100644 --- a/clojure/day10.clj +++ b/clojure/day10.clj @@ -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) @@ -32,12 +32,12 @@ 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)))] @@ -45,9 +45,9 @@ (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))])) diff --git a/clojure/day11.clj b/clojure/day11.clj index f9f98de..cc86a24 100644 --- a/clojure/day11.clj +++ b/clojure/day11.clj @@ -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])) @@ -25,7 +25,7 @@ (-> acc (update :sum + (* coeff coord)) (update :coeff + 2))) - {:sum 0 + {:sum 0 :coeff (- 1 (count galaxies))}) :sum)) @@ -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))) diff --git a/clojure/day12.clj b/clojure/day12.clj index 1599371..34de086 100644 --- a/clojure/day12.clj +++ b/clojure/day12.clj @@ -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)) @@ -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))])) diff --git a/clojure/day13.clj b/clojure/day13.clj index fd677c7..09eac55 100644 --- a/clojure/day13.clj +++ b/clojure/day13.clj @@ -8,8 +8,8 @@ (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))))) @@ -17,7 +17,7 @@ (defn mirror-line [part pattern] (aoc/find-first - (partial mirror? part pattern (rseq pattern)) + #(mirror? part pattern (rseq pattern) %) (range 1 (count pattern)))) @@ -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] diff --git a/clojure/day14.clj b/clojure/day14.clj index 324ae81..e1e7c7f 100644 --- a/clojure/day14.clj +++ b/clojure/day14.clj @@ -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)) @@ -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) @@ -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))) diff --git a/clojure/day15.clj b/clojure/day15.clj index 0e62642..74fe74f 100644 --- a/clojure/day15.clj +++ b/clojure/day15.clj @@ -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))])) diff --git a/clojure/day17.clj b/clojure/day17.clj index 582af59..b96f06f 100644 --- a/clojure/day17.clj +++ b/clojure/day17.clj @@ -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] diff --git a/clojure/day18.clj b/clojure/day18.clj index 28a6d2a..5e6baf9 100644 --- a/clojure/day18.clj +++ b/clojure/day18.clj @@ -8,10 +8,10 @@ (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) @@ -19,17 +19,17 @@ (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)) diff --git a/clojure/day19.clj b/clojure/day19.clj index 831546d..c552a7c 100644 --- a/clojure/day19.clj +++ b/clojure/day19.clj @@ -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)])) diff --git a/clojure/day20.clj b/clojure/day20.clj index 84c33d2..38fe046 100644 --- a/clojure/day20.clj +++ b/clojure/day20.clj @@ -1,26 +1,27 @@ (ns day20 (:require aoc - [clojure.string :as str])) + [clojure.string :as str] + [better-cond.core :as b])) (defn extract-name [src] (keyword (str/replace src #"%|&" ""))) (defn parse-line [line] - (let [[src dest] (str/split line #" -> ") + (let [[src dest] (str/split line #" -> ") destinations (mapv keyword (str/split dest #", ")) - amt (count destinations)] + amt (count destinations)] (case (first src) - \b [:broadcaster {:typ :b + \b [:broadcaster {:typ :b :dest destinations - :amt (inc amt)}] - \% [(extract-name src) {:typ :% - :dest destinations - :amt amt + :amt (inc amt)}] + \% [(extract-name src) {:typ :% + :dest destinations + :amt amt :pulse 0}] - \& [(extract-name src) {:typ :& - :dest destinations - :amt amt + \& [(extract-name src) {:typ :& + :dest destinations + :amt amt :pulses {}}]))) (defn init-pulses [modules] @@ -39,45 +40,47 @@ (loop [queue (conj aoc/empty-queue [:button :broadcaster 0]) state state] (if-let [[from curr v] (peek queue)] - (let [queue' (pop queue) - {:keys [typ dest amt pulse pulses]} ((:modules state) curr)] - (cond - (or (nil? typ) - (and (= typ :%) (= v 1))) - (recur queue' state) - - (= curr :broadcaster) - (recur (into queue' (map (fn [d] [curr d v]) dest)) - (update state :low-cnt + amt)) - - :else - (let [pulses' (assoc pulses from v) - pulse' (case typ - :% (- 1 pulse) - :& (if (not-any? zero? (vals pulses')) 0 1)) - zp? (zero? pulse')] - (recur (into queue' (map (fn [d] [curr d pulse']) dest)) - (cond-> state - (= typ :%) (assoc-in [:modules curr :pulse] pulse') - (= typ :&) (assoc-in [:modules curr :pulses] pulses') - zp? (update :low-cnt + amt) - (not zp?) (update :high-cnt + amt) - (and (not zp?) - (#{:ks :jf :qs :zk} curr)) ; manually found these keys - (assoc-in [:periods curr] n)))))) + #_{:clj-kondo/ignore [:unresolved-symbol]} + (b/cond + :let [queue' (pop queue) + {:keys [typ dest amt pulse pulses]} ((:modules state) curr)] + + (or (nil? typ) + (and (= typ :%) (= v 1))) + (recur queue' state) + + (= curr :broadcaster) + (recur (into queue' (map (fn [d] [curr d v]) dest)) + (update state :low-cnt + amt)) + + :let [pulses' (assoc pulses from v) + pulse' (case typ + :% (- 1 pulse) + :& (if (not-any? zero? (vals pulses')) 0 1)) + zp? (zero? pulse')] + + (recur (into queue' (map (fn [d] [curr d pulse']) dest)) + (cond-> state + (= typ :%) (assoc-in [:modules curr :pulse] pulse') + (= typ :&) (assoc-in [:modules curr :pulses] pulses') + zp? (update :low-cnt + amt) + (not zp?) (update :high-cnt + amt) + (and (not zp?) + (#{:ks :jf :qs :zk} curr)) ; manually found these keys + (assoc-in [:periods curr] n)))) state))) (defn push-button [times modules] (reduce (fn [state n] - (if (= (count (:periods state)) 4) + (if (= 4 (count (:periods state))) (reduced state) (traverse state (inc n)))) - {:modules modules - :low-cnt 0 + {:modules modules + :low-cnt 0 :high-cnt 0 - :periods {}} + :periods {}} (range times)))