about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2020-12-23 15:19:51 +0100
committerLeah Neukirchen <leah@vuxu.org>2020-12-23 15:19:51 +0100
commit4a710ae5de032f63051479b6f5953678ea3ad018 (patch)
treed40799043527250876a23a71875197c981d8b4c6
parent4878c6f4efbeaccfd588d166267357404273cef2 (diff)
downloadadventofcode2020-4a710ae5de032f63051479b6f5953678ea3ad018.tar.gz
adventofcode2020-4a710ae5de032f63051479b6f5953678ea3ad018.tar.xz
adventofcode2020-4a710ae5de032f63051479b6f5953678ea3ad018.zip
day22
-rw-r--r--day2253
-rw-r--r--day22.clj46
2 files changed, 99 insertions, 0 deletions
diff --git a/day22 b/day22
new file mode 100644
index 0000000..8c6c1e4
--- /dev/null
+++ b/day22
@@ -0,0 +1,53 @@
+Player 1:
+4
+25
+3
+11
+2
+29
+41
+23
+30
+21
+50
+8
+1
+24
+27
+10
+42
+43
+38
+15
+18
+13
+32
+37
+34
+
+Player 2:
+12
+6
+36
+35
+40
+47
+31
+9
+46
+49
+19
+16
+5
+26
+39
+48
+7
+44
+45
+20
+17
+14
+33
+28
+22
diff --git a/day22.clj b/day22.clj
new file mode 100644
index 0000000..4f95c7a
--- /dev/null
+++ b/day22.clj
@@ -0,0 +1,46 @@
+(def cards
+  (->> (clojure.string/split (slurp "day22") #"\n\n")
+       (map clojure.string/split-lines)
+       (map rest)
+       (map (partial map read-string))))
+
+(defn combat [[t1 & r1 :as p1] [t2 & r2 :as p2]]
+  (cond
+    (nil? t1) p2
+    (nil? t2) p1
+    :else
+    (if (> t1 t2)
+      (recur (concat r1 [t1 t2]) r2)
+      (recur r1 (concat r2 [t2 t1])))))
+
+(defn score [cards]
+  (->> cards
+       reverse
+       (cons 0)
+       (map-indexed vector)
+       (map (partial apply *))
+       (apply +)))
+
+(score (apply combat cards))
+;; => 33421
+
+(defn recursive-combat [log [t1 & r1 :as p1] [t2 & r2 :as p2]]
+  (if (log [p1 p2])
+    [p1 []]
+    (if (or (empty? p1) (empty? p2))
+      [p1 p2]
+      (let [log (conj log [p1 p2])]
+        (if (and (< t1 (count p1)) (< t2 (count p2)))
+          (let [[o1 o2] (recursive-combat #{} (take t1 r1) (take t2 r2))]
+            (if (empty? o2)
+              (recur log (concat r1 [t1 t2]) r2)
+              (recur log r1 (concat r2 [t2 t1]))))
+          (if (> t1 t2)
+            (recur log (concat r1 [t1 t2]) r2)
+            (recur log r1 (concat r2 [t2 t1]))))))))
+
+(->> (apply recursive-combat #{} cards)
+     flatten
+     (remove nil?)
+     score)
+;; => 33651, quite slow