Last active
December 27, 2018 14:26
-
-
Save mks-m/7b53496497de018467af9cee930f1e90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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