about summary refs log tree commit diff
path: root/day05.clj
diff options
context:
space:
mode:
Diffstat (limited to 'day05.clj')
-rw-r--r--day05.clj25
1 files changed, 9 insertions, 16 deletions
diff --git a/day05.clj b/day05.clj
index 6e5185b..0bfeb0e 100644
--- a/day05.clj
+++ b/day05.clj
@@ -7,33 +7,26 @@
        (map #(str/split % #",| -> "))
        (map (partial map parse-long))))
 
-(use 'criterium.core)
+(defn my-range [a b]
+  (case (compare a b)
+     0 (repeat a)
+    -1 (range a (inc b))
+    +1 (range a (dec b) -1)))
+
+(defn points [[x1 y1 x2 y2]]
+  (map vector (my-range x1 x2) (my-range y1 y2)))
 
 (def part1
   (->> data
        (filter (fn [[x1 y1 x2 y2]]
                  (or (= x1 x2) (= y1 y2))))
-       (mapcat (fn [[x1 y1 x2 y2]]
-                 (for [x (range (min x1 x2) (inc (max x1 x2)))
-                       y (range (min y1 y2) (inc (max y1 y2)))]
-                   [x y])))
+       (mapcat points)
        frequencies
        vals
        (filter #(< 1 %))
        count))
 ;; => 5092
 
-(defn points [[x1 y1 x2 y2]]
-  (let [dx (Long/signum (- x2 x1))
-        dy (Long/signum (- y2 y1))]
-    (loop [x x1
-           y y1
-           r []]
-      (if (and (= x2 x)
-               (= y2 y))
-        (conj r [x y])
-        (recur (+ x dx) (+ y dy) (conj r [x y]))))))
-
 (def part2
   (->> data
        (mapcat points)