Skip to content

Instantly share code, notes, and snippets.

@mks-m
Last active December 27, 2018 14:26
Show Gist options
  • Save mks-m/7b53496497de018467af9cee930f1e90 to your computer and use it in GitHub Desktop.
Save mks-m/7b53496497de018467af9cee930f1e90 to your computer and use it in GitHub Desktop.
(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