]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day13: make more concise
authorClyne Sullivan <clyne@bitgloo.com>
Mon, 13 Dec 2021 16:54:16 +0000 (11:54 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Mon, 13 Dec 2021 16:54:16 +0000 (11:54 -0500)
day13/part1.clj
day13/part2.clj

index bc7bef6fcefbbe3731290ebe325597efd98ae1e9..c3f08d4b3e8334d01183d6faef1ac06450e2f88b 100644 (file)
@@ -2,40 +2,29 @@
 
 (def input (->> (slurp "./in")
                 (str/split-lines)
-                (split-with not-empty)))
-(def points (->> (first input)
-                 (map #(str/split % #","))
-                 (map (partial map read-string))
-                 (set)))
-(def folds (->> (rest (second input))
-                (map #(str/split % #"="))
-                (map #(do [(last (first %)) (read-string (second %))]))))
+                (split-with not-empty)
+                ((juxt
+                  (fn [lst]
+                    (reduce
+                      #(conj %1 (mapv read-string (str/split %2 #",")))
+                      #{} (first lst)))
+                  (fn [lst]
+                    (->> (second lst)
+                         (drop 1)
+                         (map #(-> %
+                                   (str/split #"=")
+                                   (update 0 (comp {\x 0 \y 1} last))
+                                   (update 1 read-string)))
+                         (first)
+                         ))))))
 
-(loop [fds (take 1 folds) pts points]
-  (if (empty? fds)
-    (println (count pts))
-    (recur
-      (rest fds)
-      (let [ins (first fds) chg (second ins)]
-        (if (= \x (first ins))
-          (reduce
-            #(conj %1
-               (if (< (first %2) chg)
-                 %2
-                 (seq [(- (first %2) (* 2 (- (first %2) chg))) (second %2)])))
-            #{}
-            pts)
-          (reduce
-            #(conj %1
-               (if (< (second %2) chg)
-                 %2
-                 (seq [(first %2) (- (second %2) (* 2 (- (second %2) chg)))])
-                 ))
-            #{}
-            pts)
-          )
-        )
-      )
-    )
-  )
+(defn fold-point [idx chg pt]
+  (cond-> pt (> (get pt idx) chg) (update idx #(- % (* 2 (- % chg))))))
+
+(let [instruction (second input)]
+  (->> (first input)
+       (map (partial fold-point (first instruction) (second instruction)))
+       (distinct)
+       (count)
+       (println)))
 
index 89f011d13034bd1fac10029f80bd5223dd1cdb71..6db6555e503bb9fe696d9b6491ba256f0bb3a11b 100644 (file)
@@ -2,44 +2,32 @@
 
 (def input (->> (slurp "./in")
                 (str/split-lines)
-                (split-with not-empty)))
-(def points (->> (first input)
-                 (map #(str/split % #","))
-                 (map (partial map read-string))
-                 (set)))
-(def folds (->> (rest (second input))
-                (map #(str/split % #"="))
-                (map #(do [(last (first %)) (read-string (second %))]))))
+                (split-with not-empty)
+                ((juxt
+                  (fn [lst]
+                    (reduce
+                      #(conj %1 (mapv read-string (str/split %2 #",")))
+                      #{} (first lst)))
+                  (fn [lst]
+                    (->> (rest (second lst))
+                         (map #(-> %
+                                   (str/split #"=")
+                                   (update 0 (comp {\x 0 \y 1} last))
+                                   (update 1 read-string)))
+                         ))))))
 
 (defn print-grid [pts]
-  (doseq [p pts] (println (str/join [(first p) "," (second p)])))
-  )
+  (doseq [p pts] (println (str/join [(first p) "," (second p)]))))
 
-(loop [fds folds pts points]
-  (if (empty? fds)
-    (print-grid pts)
-    (recur
-      (rest fds)
-      (let [ins (first fds) chg (second ins)]
-        (if (= \x (first ins))
-          (reduce
-            #(conj %1
-               (if (< (first %2) chg)
-                 %2
-                 (seq [(- (first %2) (* 2 (- (first %2) chg))) (second %2)])))
-            #{}
-            pts)
-          (reduce
-            #(conj %1
-               (if (< (second %2) chg)
-                 %2
-                 (seq [(first %2) (- (second %2) (* 2 (- (second %2) chg)))])
-                 ))
-            #{}
-            pts)
-          )
-        )
-      )
-    )
-  )
+(defn fold-point [idx chg pt]
+  (cond-> pt (> (get pt idx) chg) (update idx #(- % (* 2 (- % chg))))))
+
+(print-grid
+  (apply
+    (partial
+      reduce
+      #(let [ins (first %2)]
+         (set (map (partial fold-point (first %2) (second %2)) %1))))
+    input
+    ))