about summary refs log tree commit diff
path: root/day13.clj
diff options
context:
space:
mode:
Diffstat (limited to 'day13.clj')
-rw-r--r--day13.clj38
1 files changed, 38 insertions, 0 deletions
diff --git a/day13.clj b/day13.clj
new file mode 100644
index 0000000..83d6611
--- /dev/null
+++ b/day13.clj
@@ -0,0 +1,38 @@
+(ns org.vuxu.aoc2021.day13
+  (:require [clojure.string :as str]
+            [clojure.set :as set]))
+
+(let [[part1 part2] (str/split (slurp "day13") #"\n\n")]
+  (def dots (set (for [line (str/split-lines part1)]
+                   (mapv parse-long (str/split line #",")))))
+  (def folds (for [line (str/split-lines part2)]
+               (let [[xy n] (take-last 2 (str/split line #" |="))]
+                 [(symbol xy)
+                  (parse-long n)]))))
+
+(defn fold [dots [xy n]]
+  (let [sel (case xy
+              x 0
+              y 1)
+        {a false b true} (group-by #(> (get % sel) n) dots)
+        a' (for [coord b]
+                 (update coord sel #(- n (- % n))))]
+    (set/union (set a) (set a'))))
+
+(def part1
+  (count (fold dots (first folds))))
+;; => 716
+
+(defn show [dots]
+  (str/join "\n"
+            (reverse
+             (for [x (range (inc (apply max (map first dots))))]
+               (apply str
+                      (for [y (range (inc (apply max (map second dots))))]
+                        (if (dots [x y]) "#" ".")))))))
+
+(def part2
+  (show (reduce fold dots folds)))
+
+(print "---\n" part2 "\n---\n")
+;; RPCKFBLR