about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2020-12-24 14:37:30 +0100
committerLeah Neukirchen <leah@vuxu.org>2020-12-24 14:38:00 +0100
commit69a013313ee0399e797363bbe76d9b32747c723f (patch)
tree2f7af1cfedf969b1c87b35ce1153179bc6f0ecc4
parent4a710ae5de032f63051479b6f5953678ea3ad018 (diff)
downloadadventofcode2020-69a013313ee0399e797363bbe76d9b32747c723f.tar.gz
adventofcode2020-69a013313ee0399e797363bbe76d9b32747c723f.tar.xz
adventofcode2020-69a013313ee0399e797363bbe76d9b32747c723f.zip
day23
-rw-r--r--day23.clj70
-rw-r--r--day23.ijs11
2 files changed, 81 insertions, 0 deletions
diff --git a/day23.clj b/day23.clj
new file mode 100644
index 0000000..5481e13
--- /dev/null
+++ b/day23.clj
@@ -0,0 +1,70 @@
+(def data
+  (->> "158937462"
+       (map str)
+       (map read-string)))
+
+(defn split-after [e l]
+  (let [[h t] (split-with (partial not= e) l)]
+    [(concat h [(first t)]) (rest t)]))
+
+(defn first-smaller [n l]
+  (if (< n 1)
+    (first-smaller (reduce max l) l)
+    (if (some #{n} l)
+      n
+      (first-smaller (dec n) l))))
+
+(defn step [data]
+  (let [[c t1 t2 t3 & r] data
+        d (first-smaller (dec c) r)
+        [h t] (split-after d r)]
+    (concat h [t1 t2 t3] t [c])))
+
+(defn final [data]
+  (let [[h t] (split-with (partial not= 1) data)]
+    (concat (rest t) h)))
+
+(->> data
+     (iterate step)
+     (#(nth % 101))
+     final
+     (apply str))
+;; => "69473825"
+
+(def data2
+  (concat data (range 10 1000001)))
+
+(defn link-up [l]
+  (->> l
+       (partition 2 1)
+       (reduce #(apply assoc %1 %2)
+               (vec (repeat (inc (count l)) nil)))
+       (#(assoc %
+                (last l) (first l)
+                0 (first l)))))
+
+(defn first-smaller2 [val seen]
+  (if (seen val)
+    (recur (mod (dec val) 1000001) seen)
+    val))
+
+(defn step2 [state]
+  (let [cur (state 0)
+        a (state cur)
+        b (state a)
+        c (state b)
+        n (state c)
+        cval (first-smaller2 (dec cur) #{a b c 0})
+        ins (state cval)]
+    (assoc state
+           0 n
+           cur n
+           cval a
+           c ins)))
+
+(->> data2
+     link-up
+     (iterate step2)
+     (#(nth % 10000001))
+     (#(* (% 1) (% (% 1)))))
+;; => 96604396189
diff --git a/day23.ijs b/day23.ijs
new file mode 100644
index 0000000..0a58d46
--- /dev/null
+++ b/day23.ijs
@@ -0,0 +1,11 @@
+d =: 10 #.inv 158937462
+
+step =: 3 : 0
+'c h t' =. ({. ; }.@(4&{.) ; 4&}.) y
+'a b' =. t ({. ; }.)~ >: {. /: (|. (<:c) |. 1+i.9) i. t
+a,h,b,c
+)
+
+,":"0}.(]|.~I.@(1&=)) step^:100 d
+
+NB. part2 TBD