day5: learned update and reduce

master
Clyne 3 years ago
parent 4f63f785fa
commit 68c02872a8

@ -14,45 +14,35 @@
) )
) )
(defn mark-coord [cmap x y] (defn read-all-coords []
(vec (loop [cds [] c (read-coords)]
(for [c (range 0 (count cmap))] (if (empty? c)
(if (= c y) cds
(vec (recur
(for [r (range 0 (count cmap))] (conj cds c)
(if (= r x) (read-coords)
(inc (get (get cmap c) r))
(get (get cmap c) r)
)
)
)
(get cmap c)
) )
) )
) )
) )
(defn mark-coord [cmap x y]
(update cmap y #(update % x inc))
)
(defn mark-coords [cmap x1 y1 x2 y2] (defn mark-coords [cmap x1 y1 x2 y2]
(cond (cond
(= y1 y2) (= y1 y2)
(loop [cm cmap x (range (min x1 x2) (inc (max x1 x2)))] (reduce
(if (empty? x) #(mark-coord %1 %2 y1)
cm cmap
(recur (range (min x1 x2) (inc (max x1 x2)))
(mark-coord cm (first x) y1)
(rest x)
)
)
) )
(= x1 x2) (= x1 x2)
(loop [cm cmap y (range (min y1 y2) (inc (max y1 y2)))] (reduce
(if (empty? y) #(mark-coord %1 x1 %2)
cm cmap
(recur (range (min y1 y2) (inc (max y1 y2)))
(mark-coord cm x1 (first y))
(rest y)
)
)
) )
:else :else
cmap cmap
@ -68,14 +58,10 @@
) )
(def finished-map (def finished-map
(loop [cmap (empty-map) coord (read-coords)] (reduce
(if (empty? coord) #(apply (partial mark-coords %1) %2)
cmap (empty-map)
(recur (read-all-coords)
(apply (partial mark-coords cmap) coord)
(read-coords)
)
)
) )
) )

@ -14,45 +14,35 @@
) )
) )
(defn mark-coord [cmap x y] (defn read-all-coords []
(vec (loop [cds [] c (read-coords)]
(for [c (range 0 (count cmap))] (if (empty? c)
(if (= c y) cds
(vec (recur
(for [r (range 0 (count cmap))] (conj cds c)
(if (= r x) (read-coords)
(inc (get (get cmap c) r))
(get (get cmap c) r)
)
)
)
(get cmap c)
) )
) )
) )
) )
(defn mark-coord [cmap x y]
(update cmap y #(update % x inc))
)
(defn mark-coords [cmap x1 y1 x2 y2] (defn mark-coords [cmap x1 y1 x2 y2]
(cond (cond
(= y1 y2) (= y1 y2)
(loop [cm cmap x (range (min x1 x2) (inc (max x1 x2)))] (reduce
(if (empty? x) #(mark-coord %1 %2 y1)
cm cmap
(recur (range (min x1 x2) (inc (max x1 x2)))
(mark-coord cm (first x) y1)
(rest x)
)
)
) )
(= x1 x2) (= x1 x2)
(loop [cm cmap y (range (min y1 y2) (inc (max y1 y2)))] (reduce
(if (empty? y) #(mark-coord %1 x1 %2)
cm cmap
(recur (range (min y1 y2) (inc (max y1 y2)))
(mark-coord cm x1 (first y))
(rest y)
)
)
) )
:else :else
(let [ic (if (< x1 x2) [x1 y1] [x2 y2]) (let [ic (if (< x1 x2) [x1 y1] [x2 y2])
@ -81,14 +71,10 @@
) )
(def finished-map (def finished-map
(loop [cmap (empty-map) coord (read-coords)] (reduce
(if (empty? coord) #(apply (partial mark-coords %1) %2)
cmap (empty-map)
(recur (read-all-coords)
(apply (partial mark-coords cmap) coord)
(read-coords)
)
)
) )
) )

Loading…
Cancel
Save