-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.clj
24 lines (20 loc) · 824 Bytes
/
06.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(ns universal-orbit-map
(:require [clojure.string :as str]
[clojure.set]))
;;; part 1
(def orbit-map (->> (slurp "06.in")
(str/split-lines)
(map (fn [s] (map keyword (str/split s #"\)"))))
(reduce (fn [m [orbited orbits]] (assoc m orbits orbited)) {})))
(defn orbits-number [m object]
(loop [object object, number 0]
(if (object m)
(recur (object m) (inc number))
number)))
(let [objects (set (keys orbit-map))]
(apply + (map (partial orbits-number orbit-map) objects)))
;;; part 2
(- (count (filter (fn [[object freq]] (= 1 freq))
(frequencies (concat (take-while #(not= :COM %) (iterate orbit-map :YOU))
(take-while #(not= :COM %) (iterate orbit-map :SAN))))))
2)