Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns clojure1.core
- (:gen-class))
- (defn hexcode-s [] (-> "C:\\Users\\ewaka\\Desktop\\hexcode.txt"
- slurp
- clojure.string/trim))
- (defn hexcode [string] (-> string
- (BigInteger. 16)
- bigint))
- (defn cnt [hex-string] (* 4 (count hex-string)))
- (defn num-to-bits [num cnt]
- (loop [num num
- bits (list)]
- (if (zero? num)
- (into bits (take (- cnt (count bits)) (repeat 0)))
- (recur (quot num 2) (conj bits (int (rem num 2)))))))
- (defprotocol Packet
- (evaluate [this] "get the value of the form")
- (sum-versions [this] "get the sum of this packet's and its children's versions"))
- (defrecord Literal [version id value]
- Packet
- (evaluate [this] value)
- (sum-versions [this] version))
- (defrecord Operation [version id fun operands]
- Packet
- (evaluate [this] (fun (map evaluate operands)))
- (sum-versions [this] (apply + version (map sum-versions operands))))
- (defn digits-to-num [base coll]
- (->> coll
- reverse
- (map * (iterate #(* base %) 1))
- (apply +)))
- (defmulti parse
- (fn [coll] (digits-to-num 2 (take 3 (drop 3 coll)))))
- (defmulti parse-operands
- (fn [coll] (first (drop 6 coll))))
- (defmethod parse-operands 0 [collection]
- (let [num-elems (digits-to-num 2 (take 15 (drop 7 collection)))]
- (loop [coll (drop 22 collection)
- n num-elems
- symbols []]
- (if (zero? n)
- [symbols coll]
- (let [[symbol new-coll] (parse coll)]
- (recur new-coll
- (- n (- (count coll) (count new-coll)))
- (conj symbols symbol)))))))
- (defmethod parse-operands 1 [collection]
- (let [num-elems (digits-to-num 2 (take 11 (drop 7 collection)))]
- (loop [coll (drop 18 collection)
- n num-elems
- symbols []]
- (if (zero? n)
- [symbols coll]
- (let [[symbol new-coll] (parse coll)]
- (recur new-coll
- (dec n)
- (conj symbols symbol)))))))
- (defmethod parse 4 [collection]
- (let [version (digits-to-num 2 (take 3 collection))
- id (digits-to-num 2 (take 3 (drop 3 collection)))]
- (loop [coll (drop 6 collection)
- bits []]
- (if (zero? (first coll))
- [(->Literal version id (digits-to-num 2 (into bits (take 4 (rest coll)))))
- (drop 5 coll)]
- (recur (drop 5 coll) (into bits (take 4 (rest coll))))))))
- (def functions {0 #(apply + %)
- 1 #(apply * %)
- 2 #(apply min %)
- 3 #(apply max %)
- 5 (fn [[a b]] (if (> a b) 1 0))
- 6 (fn [[a b]] (if (< a b) 1 0))
- 7 (fn [[a b]] (if (= a b) 1 0))})
- (defmethod parse :default [collection]
- (let [version (digits-to-num 2 (take 3 collection))
- id (digits-to-num 2 (take 3 (drop 3 collection)))]
- (let [[operands coll] (parse-operands collection)]
- [(->Operation version id (get functions id) operands) coll])))
- (defn parse-packets [coll]
- (first (parse coll)))
- (defn answer16-1 []
- (let [hexcode-s (hexcode-s)]
- (-> (num-to-bits (hexcode hexcode-s) (cnt hexcode-s))
- parse-packets
- sum-versions)))
- (defn answer16-2 []
- (let [hexcode-s (hexcode-s)]
- (-> (num-to-bits (hexcode hexcode-s) (cnt hexcode-s))
- parse-packets
- evaluate)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement