Skip to content

Instantly share code, notes, and snippets.

@ihabunek
Created December 14, 2017 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihabunek/927e8534bef011dd9c14f0284153e678 to your computer and use it in GitHub Desktop.
Save ihabunek/927e8534bef011dd9c14f0284153e678 to your computer and use it in GitHub Desktop.
(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