Created
December 14, 2017 15:49
-
-
Save ihabunek/927e8534bef011dd9c14f0284153e678 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 aoc2017.foo) | |
(def node-map | |
"A tree structure defined as a map" | |
{:a [:b :c] | |
:b [:d :e :f] | |
:e [:g :h :i] | |
:i [:j :k]}) | |
(defn count-nodes | |
"Returns the number of nodes located under the given node (inclusive)." | |
[node-map node] | |
(let [children (get node-map node)] | |
(if (empty? children) 1 | |
(inc | |
(reduce + | |
(map #(count-nodes node-map %) children)))))) | |
(defn main [] | |
(println (count-nodes node-map :b))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment