]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
add day 13
authorClyne Sullivan <clyne@bitgloo.com>
Mon, 13 Dec 2021 13:01:32 +0000 (08:01 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Mon, 13 Dec 2021 13:01:32 +0000 (08:01 -0500)
day13/part1.clj [new file with mode: 0644]
day13/part2.clj [new file with mode: 0644]

diff --git a/day13/part1.clj b/day13/part1.clj
new file mode 100644 (file)
index 0000000..bc7bef6
--- /dev/null
@@ -0,0 +1,41 @@
+(require '[clojure.string :as str])
+
+(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 %))]))))
+
+(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)
+          )
+        )
+      )
+    )
+  )
+
diff --git a/day13/part2.clj b/day13/part2.clj
new file mode 100644 (file)
index 0000000..89f011d
--- /dev/null
@@ -0,0 +1,45 @@
+(require '[clojure.string :as str])
+
+(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 %))]))))
+
+(defn print-grid [pts]
+  (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)
+          )
+        )
+      )
+    )
+  )
+