Advertisement
Guest User

Day 16 - Clojure

a guest
Dec 16th, 2021
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns clojure1.core
  2.   (:gen-class))
  3.  
  4. (defn hexcode-s [] (-> "C:\\Users\\ewaka\\Desktop\\hexcode.txt"
  5.                        slurp
  6.                        clojure.string/trim))
  7.  
  8. (defn hexcode [string] (-> string
  9.                            (BigInteger. 16)
  10.                            bigint))
  11.  
  12. (defn cnt [hex-string] (* 4 (count hex-string)))
  13.  
  14.  
  15.  
  16. (defn num-to-bits [num cnt]
  17.   (loop [num num
  18.          bits (list)]
  19.     (if (zero? num)
  20.       (into bits (take (- cnt (count bits)) (repeat 0)))
  21.       (recur (quot num 2) (conj bits (int (rem num 2)))))))
  22.  
  23. (defprotocol Packet
  24.   (evaluate [this] "get the value of the form")
  25.   (sum-versions [this] "get the sum of this packet's and its children's versions"))
  26.  
  27. (defrecord Literal [version id value]
  28.   Packet
  29.   (evaluate [this] value)
  30.   (sum-versions [this] version))
  31.  
  32. (defrecord Operation [version id fun operands]
  33.   Packet
  34.   (evaluate [this] (fun (map evaluate operands)))
  35.   (sum-versions [this] (apply + version (map sum-versions operands))))
  36.  
  37. (defn digits-to-num [base coll]
  38.   (->> coll
  39.        reverse
  40.        (map * (iterate #(* base %) 1))
  41.        (apply +)))
  42.  
  43. (defmulti parse
  44.   (fn [coll] (digits-to-num 2 (take 3 (drop 3 coll)))))
  45.  
  46. (defmulti parse-operands
  47.   (fn [coll] (first (drop 6 coll))))
  48.  
  49. (defmethod parse-operands 0 [collection]
  50.   (let [num-elems (digits-to-num 2 (take 15 (drop 7 collection)))]
  51.     (loop [coll (drop 22 collection)
  52.            n num-elems
  53.            symbols []]
  54.       (if (zero? n)
  55.         [symbols coll]
  56.         (let [[symbol new-coll] (parse coll)]
  57.           (recur new-coll
  58.                  (- n (- (count coll) (count new-coll)))
  59.                  (conj symbols symbol)))))))
  60.  
  61. (defmethod parse-operands 1 [collection]
  62.   (let [num-elems (digits-to-num 2 (take 11 (drop 7 collection)))]
  63.     (loop [coll (drop 18 collection)
  64.            n num-elems
  65.            symbols []]
  66.       (if (zero? n)
  67.         [symbols coll]
  68.         (let [[symbol new-coll] (parse coll)]
  69.           (recur new-coll
  70.                  (dec n)
  71.                  (conj symbols symbol)))))))
  72.  
  73. (defmethod parse 4 [collection]
  74.   (let [version (digits-to-num 2 (take 3 collection))
  75.         id (digits-to-num 2 (take 3 (drop 3 collection)))]
  76.     (loop [coll (drop 6 collection)
  77.            bits []]
  78.       (if (zero? (first coll))
  79.         [(->Literal version id (digits-to-num 2 (into bits (take 4 (rest coll)))))
  80.          (drop 5 coll)]
  81.         (recur (drop 5 coll) (into bits (take 4 (rest coll))))))))
  82.  
  83.  
  84. (def functions {0 #(apply + %)
  85.                 1 #(apply * %)
  86.                 2 #(apply min %)
  87.                 3 #(apply max %)
  88.                 5 (fn [[a b]] (if (> a b) 1 0))
  89.                 6 (fn [[a b]] (if (< a b) 1 0))
  90.                 7 (fn [[a b]] (if (= a b) 1 0))})
  91.  
  92.  
  93. (defmethod parse :default [collection]
  94.   (let [version (digits-to-num 2 (take 3 collection))
  95.         id (digits-to-num 2 (take 3 (drop 3 collection)))]
  96.     (let [[operands coll] (parse-operands collection)]
  97.       [(->Operation version id (get functions id) operands) coll])))
  98.  
  99. (defn parse-packets [coll]
  100.   (first (parse coll)))
  101.  
  102.  
  103. (defn answer16-1 []
  104.   (let [hexcode-s (hexcode-s)]
  105.     (-> (num-to-bits (hexcode hexcode-s) (cnt hexcode-s))
  106.         parse-packets
  107.         sum-versions)))
  108.  
  109. (defn answer16-2 []
  110.   (let [hexcode-s (hexcode-s)]
  111.     (-> (num-to-bits (hexcode hexcode-s) (cnt hexcode-s))
  112.         parse-packets
  113.         evaluate)))
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement