about summary refs log tree commit diff
path: root/day24.clj
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2021-12-25 17:51:49 +0100
committerLeah Neukirchen <leah@vuxu.org>2021-12-25 17:51:49 +0100
commitceb09f9791e37f5c7fc422c9f848de09d58a7337 (patch)
tree5e4c9a3ab4c4d851ce53e1e008fb6886e0ec4e61 /day24.clj
parent01eae09c06e680f5ad9c7845d6ad33bcac458330 (diff)
downloadadventofcode2021-ceb09f9791e37f5c7fc422c9f848de09d58a7337.tar.gz
adventofcode2021-ceb09f9791e37f5c7fc422c9f848de09d58a7337.tar.xz
adventofcode2021-ceb09f9791e37f5c7fc422c9f848de09d58a7337.zip
day24
Diffstat (limited to 'day24.clj')
-rw-r--r--day24.clj52
1 files changed, 52 insertions, 0 deletions
diff --git a/day24.clj b/day24.clj
new file mode 100644
index 0000000..45e2f0a
--- /dev/null
+++ b/day24.clj
@@ -0,0 +1,52 @@
+(ns org.vuxu.aoc2021.day24
+  (:require [clojure.string :as str]
+            [clojure.core.match :refer [match]]
+            [clojure.set :as set]))
+
+(def data
+  (->> (slurp "day24")
+       str/split-lines
+       (map #(str/split % #" "))
+       (map (partial mapv #(if (re-matches #"-?\d+" %) (parse-long %) %)))))
+
+(def params
+  (for [part (partition (/ (count data) 14) data)]
+    [(last (nth part 5))
+     (last (nth part 15))]))
+
+(defn run [z d c1 c2]
+  (let [con (not= (+ (mod z 26) c1) d)
+        z (if (neg? c1) (quot z 26) z)
+        z (if con (+ (* 26 z) d c2) z)]
+    z))
+
+(def part1
+  (->> (reduce (fn [acc [c1 c2]]
+                 (prn [c1 c2 (count acc)])
+                 (into (sorted-map)
+                       (for [[z v] acc
+                             d (range 1 10)
+                             :when (<= z (* 26 26 26 26 26))]
+                         [(run z d c1 c2) (conj v d)]))
+                 )
+               {0 []}
+               params)
+       (#(get % 0))
+       (apply str)))
+;; => "96299896449997"
+
+(def part2
+  (->> (reduce (fn [acc [c1 c2]]
+                 (prn [c1 c2 (count acc)])
+                 (into (sorted-map)
+                       (for [[z v] acc
+                             :let [z (- z)]
+                             d (reverse (range 1 10))
+                             :when (<= z (* 26 26 26 26 26))]
+                         [(- (run z d c1 c2)) (conj v d)]))
+                 )
+               (sorted-map 0 [])
+               params)
+       (#(get % 0))
+       (apply str)))
+;; => "31162141116841"