about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2021-12-17 17:02:29 +0100
committerLeah Neukirchen <leah@vuxu.org>2021-12-17 17:02:29 +0100
commit4e8974fb4f97a9535fb51bb7b23cd65025f05ce1 (patch)
treebcdac420d02e9fc5afa346881078963eea022169
parentbfdca20a589f8db3821b2b053e75a89e8eac08e5 (diff)
downloadadventofcode2021-4e8974fb4f97a9535fb51bb7b23cd65025f05ce1.tar.gz
adventofcode2021-4e8974fb4f97a9535fb51bb7b23cd65025f05ce1.tar.xz
adventofcode2021-4e8974fb4f97a9535fb51bb7b23cd65025f05ce1.zip
day16
-rw-r--r--day161
-rw-r--r--day16.clj101
2 files changed, 102 insertions, 0 deletions
diff --git a/day16 b/day16
new file mode 100644
index 0000000..4eb7228
--- /dev/null
+++ b/day16
@@ -0,0 +1 @@
+005532447836402684AC7AB3801A800021F0961146B1007A1147C89440294D005C12D2A7BC992D3F4E50C72CDF29EECFD0ACD5CC016962099194002CE31C5D3005F401296CAF4B656A46B2DE5588015C913D8653A3A001B9C3C93D7AC672F4FF78C136532E6E0007FCDFA975A3004B002E69EC4FD2D32CDF3FFDDAF01C91FCA7B41700263818025A00B48DEF3DFB89D26C3281A200F4C5AF57582527BC1890042DE00B4B324DBA4FAFCE473EF7CC0802B59DA28580212B3BD99A78C8004EC300761DC128EE40086C4F8E50F0C01882D0FE29900A01C01C2C96F38FCBB3E18C96F38FCBB3E1BCC57E2AA0154EDEC45096712A64A2520C6401A9E80213D98562653D98562612A06C0143CB03C529B5D9FD87CBA64F88CA439EC5BB299718023800D3CE7A935F9EA884F5EFAE9E10079125AF39E80212330F93EC7DAD7A9D5C4002A24A806A0062019B6600730173640575A0147C60070011FCA005000F7080385800CBEE006800A30C023520077A401840004BAC00D7A001FB31AAD10CC016923DA00686769E019DA780D0022394854167C2A56FB75200D33801F696D5B922F98B68B64E02460054CAE900949401BB80021D0562344E00042A16C6B8253000600B78020200E44386B068401E8391661C4E14B804D3B6B27CFE98E73BCF55B65762C402768803F09620419100661EC2A8CE0008741A83917CC024970D9E718DD341640259D80200008444D8F713C401D88310E2EC9F20F3330E059009118019A8803F12A0FC6E1006E3744183D27312200D4AC01693F5A131C93F5A131C970D6008867379CD3221289B13D402492EE377917CACEDB3695AD61C939C7C10082597E3740E857396499EA31980293F4FD206B40123CEE27CFB64D5E57B9ACC7F993D9495444001C998E66B50896B0B90050D34DF3295289128E73070E00A4E7A389224323005E801049351952694C000
diff --git a/day16.clj b/day16.clj
new file mode 100644
index 0000000..a4d8925
--- /dev/null
+++ b/day16.clj
@@ -0,0 +1,101 @@
+(ns org.vuxu.aoc2021.day16
+  (:require [clojure.string :as str]
+            [clojure.pprint :refer [cl-format]]))
+
+(defn hex2bin [hex]
+  (map #(Character/digit % 2)
+       (mapcat #(cl-format nil "~4,'0B" (Character/digit % 16)) hex)))
+
+(defn bin2int [bin]
+  (Integer/parseInt (apply str bin) 2))
+
+(def data
+  (->> (slurp "day16")
+       str/trim-newline
+       hex2bin))
+
+(declare parse-all)
+(declare parse-n)
+
+(defn parse [bin]
+  (let [[version bin] (split-at 3 bin)
+        [type bin] (split-at 3 bin)
+        version (bin2int version)
+        type (bin2int type)
+        
+        [content bin] (if (= type 4)
+                        (loop [bin bin
+                               lit 0]
+                          (let [[cont bin] (split-at 1 bin)
+                                [data bin] (split-at 4 bin)
+                                lit (bit-or (bit-shift-left lit 4)
+                                            (bin2int data))]
+                            (if (= cont [1])
+                              (recur bin lit)
+                              [{:literal lit} bin])))
+                        (let [[length-type bin] (split-at 1 bin)
+                              [inner-packets bin]
+                              (if (= length-type [0])
+                                (let [[l bin] (split-at 15 bin)
+                                      [packets bin]
+                                      (split-at (bin2int l) bin)]
+                                  [(parse-all packets) bin])
+                                (let [[n bin] (split-at 11 bin)]
+                                  (parse-n bin (bin2int n))))
+                              ]
+                          [{:operator inner-packets} bin]))
+        ]
+    [(into {:version version :type type} content)
+     bin]))
+
+(defn parse-all [bin]
+  (loop [bin bin
+         parsed []]
+    (if (seq bin)
+      (let [[item bin] (parse bin)]
+        (recur bin (conj parsed item)))
+      parsed)))
+
+(defn parse-n [bin n]
+  (loop [bin bin
+         n n
+         parsed []]
+    (if (zero? n)
+      [parsed bin]
+      (let [[item bin] (parse bin)]
+        (recur bin (dec n) (conj parsed item))))))
+
+(defn sum-version [packet]
+  (if (:literal packet)
+    (:version packet)
+    (+ (:version packet)
+       (apply + (map sum-version (:operator packet))))))
+
+(def part1
+  (sum-version (first (parse data))))
+;; => 981
+
+(defn eval-packet [packet]
+  (prn packet)
+  (case (:type packet)
+    0 (apply + (map eval-packet (:operator packet)))
+    1 (apply * (map eval-packet (:operator packet)))
+    2 (apply min (map eval-packet (:operator packet)))
+    3 (apply max (map eval-packet (:operator packet)))
+    4 (:literal packet)
+    5 (if (> (eval-packet (first (:operator packet)))
+             (eval-packet (second (:operator packet))))
+        1
+        0)
+    6 (if (< (eval-packet (first (:operator packet)))
+             (eval-packet (second (:operator packet))))
+        1
+        0)
+    7 (if (= (eval-packet (first (:operator packet)))
+             (eval-packet (second (:operator packet))))
+        1
+        0)))
+
+(def part2
+  (eval-packet (first (parse data))))
+;; => 299227024091