Skip to content

Instantly share code, notes, and snippets.

@keymone keymone/d8.clj
Last active Dec 27, 2018

Embed
What would you like to do?
(ns d8)
(defonce d8-input (slurp "d8.txt"))
(defonce d8-input-list (read-string (str "[" d8-input "]")))
(defn read-node [[cn mn & data]]
(let [[ccol rest]
(loop [ccol [] rest data cn cn]
(if (= 0 cn)
[ccol rest]
(let [[node rest] (read-node rest)]
(recur (conj ccol [node rest]) rest (dec cn)))))]
[{:cc ccol :mc (take mn rest)} (drop mn rest)]))
(defonce d8-tree (read-node d8-input-list))
(defn count-md [[{:keys [cc mc]}]]
(reduce + (concat mc (map count-md cc))))
(defn node-val [[{:keys [cc mc]}]]
(if (empty? cc)
(reduce + mc)
(reduce + (map #(node-val (nth cc (dec %) [{:cc []}])) mc))))
(println (count-md d8-tree))
(println (node-val d8-tree))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.