aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2021-12-05 10:23:12 -0500
committerClyne Sullivan <clyne@bitgloo.com>2021-12-05 10:23:12 -0500
commit68c02872a81d598b5067200854f9c46fd9f06876 (patch)
tree92326414cb97c36b31c61cc8f672d8d246c95f48
parent4f63f785fa9f8eecea846c9b75d7f63453041ef2 (diff)
day5: learned update and reduce
-rw-r--r--day5/part1.clj60
-rw-r--r--day5/part2.clj60
2 files changed, 46 insertions, 74 deletions
diff --git a/day5/part1.clj b/day5/part1.clj
index 02eb846..95d04ba 100644
--- a/day5/part1.clj
+++ b/day5/part1.clj
@@ -14,45 +14,35 @@
)
)
-(defn mark-coord [cmap x y]
- (vec
- (for [c (range 0 (count cmap))]
- (if (= c y)
- (vec
- (for [r (range 0 (count cmap))]
- (if (= r x)
- (inc (get (get cmap c) r))
- (get (get cmap c) r)
- )
- )
- )
- (get cmap c)
+(defn read-all-coords []
+ (loop [cds [] c (read-coords)]
+ (if (empty? c)
+ cds
+ (recur
+ (conj cds c)
+ (read-coords)
)
)
)
)
+(defn mark-coord [cmap x y]
+ (update cmap y #(update % x inc))
+ )
+
(defn mark-coords [cmap x1 y1 x2 y2]
(cond
(= y1 y2)
- (loop [cm cmap x (range (min x1 x2) (inc (max x1 x2)))]
- (if (empty? x)
- cm
- (recur
- (mark-coord cm (first x) y1)
- (rest x)
- )
- )
+ (reduce
+ #(mark-coord %1 %2 y1)
+ cmap
+ (range (min x1 x2) (inc (max x1 x2)))
)
(= x1 x2)
- (loop [cm cmap y (range (min y1 y2) (inc (max y1 y2)))]
- (if (empty? y)
- cm
- (recur
- (mark-coord cm x1 (first y))
- (rest y)
- )
- )
+ (reduce
+ #(mark-coord %1 x1 %2)
+ cmap
+ (range (min y1 y2) (inc (max y1 y2)))
)
:else
cmap
@@ -68,14 +58,10 @@
)
(def finished-map
- (loop [cmap (empty-map) coord (read-coords)]
- (if (empty? coord)
- cmap
- (recur
- (apply (partial mark-coords cmap) coord)
- (read-coords)
- )
- )
+ (reduce
+ #(apply (partial mark-coords %1) %2)
+ (empty-map)
+ (read-all-coords)
)
)
diff --git a/day5/part2.clj b/day5/part2.clj
index af63683..c374716 100644
--- a/day5/part2.clj
+++ b/day5/part2.clj
@@ -14,45 +14,35 @@
)
)
-(defn mark-coord [cmap x y]
- (vec
- (for [c (range 0 (count cmap))]
- (if (= c y)
- (vec
- (for [r (range 0 (count cmap))]
- (if (= r x)
- (inc (get (get cmap c) r))
- (get (get cmap c) r)
- )
- )
- )
- (get cmap c)
+(defn read-all-coords []
+ (loop [cds [] c (read-coords)]
+ (if (empty? c)
+ cds
+ (recur
+ (conj cds c)
+ (read-coords)
)
)
)
)
+(defn mark-coord [cmap x y]
+ (update cmap y #(update % x inc))
+ )
+
(defn mark-coords [cmap x1 y1 x2 y2]
(cond
(= y1 y2)
- (loop [cm cmap x (range (min x1 x2) (inc (max x1 x2)))]
- (if (empty? x)
- cm
- (recur
- (mark-coord cm (first x) y1)
- (rest x)
- )
- )
+ (reduce
+ #(mark-coord %1 %2 y1)
+ cmap
+ (range (min x1 x2) (inc (max x1 x2)))
)
(= x1 x2)
- (loop [cm cmap y (range (min y1 y2) (inc (max y1 y2)))]
- (if (empty? y)
- cm
- (recur
- (mark-coord cm x1 (first y))
- (rest y)
- )
- )
+ (reduce
+ #(mark-coord %1 x1 %2)
+ cmap
+ (range (min y1 y2) (inc (max y1 y2)))
)
:else
(let [ic (if (< x1 x2) [x1 y1] [x2 y2])
@@ -81,14 +71,10 @@
)
(def finished-map
- (loop [cmap (empty-map) coord (read-coords)]
- (if (empty? coord)
- cmap
- (recur
- (apply (partial mark-coords cmap) coord)
- (read-coords)
- )
- )
+ (reduce
+ #(apply (partial mark-coords %1) %2)
+ (empty-map)
+ (read-all-coords)
)
)