]> localhost Git - adventofcode2019.git/commitdiff
Fix day6pt1 and add correct day6pt2
authorJack Kinsey <j.jameskinsey@gmail.com>
Sat, 7 Dec 2019 05:04:44 +0000 (00:04 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Sat, 7 Dec 2019 05:04:44 +0000 (00:04 -0500)
project.clj
src/adventofcode2019/day06.clj
src/adventofcode2019/template.clj

index fe88a58906cbf459f60fe9737b29c53fc392ce8c..df6ecc57b62170e6f34de7c9732ea2bd784ccb9e 100644 (file)
@@ -4,6 +4,7 @@
 ;;:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
 ;;          :url "https://www.eclipse.org/legal/epl-2.0/"}
   :dependencies [[org.clojure/clojure "1.10.0"]
+                 [org.clojure/core.match "0.3.0"]
                  [org.clojure/math.combinatorics "0.1.6"]]
   :main ^:skip-aot adventofcode2019.core
   :target-path "target/%s"
index 1d67336dface1692472d25e4d1ea4c5ea91328b8..928869288f21f81904806631ab243fadbdebed2a 100644 (file)
@@ -2,16 +2,37 @@
     [:require [adventofcode2019.lib :refer :all]
               [clojure.string :as str]])
 
-;; FIXME: is the input guaranteed in-order?
 (defn a-orbits-b [orbits-map [b a]]
   (if (contains? orbits-map b)
-    (-> (assoc orbits-map a {:parent b, :children [], :depth (inc (:depth b))})
+    (-> (assoc orbits-map a {:parent b, :children (get-in orbits-map [a :children] [])})
         (update-in [b :children] conj a))
-    (-> (assoc orbits-map b {:parent nil, :children [], :depth 0})
+    (-> (assoc orbits-map b {:parent nil, :children []})
         (a-orbits-b [b a]))))
 
+(defn distances-from [tree node]
+  (letfn [(path-rc [dist tree node]
+            (let [seen? #(some? (get-in tree [% :dist]))
+                  neighbors (remove (some-fn nil? seen?) 
+                                    (cons (get-in tree [node :parent]) 
+                                          (get-in tree [node :children])))]
+              (as-> tree it 
+                    (assoc-in it [node :dist] dist)
+                    (reduce #(path-rc (inc dist) %1 %2) it neighbors))))]
+    (path-rc 0 tree node)))
+
+(defn assign-depth [tree]
+  (let [is-root? #(nil? (get-in tree [% :parent]))
+        root (first (filter is-root? (keys tree)))]
+    (letfn [(depth-rc [depth tree root]
+              (as-> tree it 
+                    (assoc-in it [root :depth] depth)
+                    (reduce #(depth-rc (inc depth) %1 %2) 
+                            it (get-in tree [root :children]))))]
+      (depth-rc 0 tree root))))
+
 (defn day06 []
-  (let [input (map #(str/split % #")") (get-list-from-file (input-file)))
-        orbits-map (reduce a-orbits-b {} input)] 
-    (part1 (reduce + (map :depth orbits-map)))
-    #_(part2)))
+  (let [input (map #(str/split % #"\)") (get-list-from-file (input-file)))
+        orbits-map (reduce a-orbits-b {} input)
+        with-depths (assign-depth orbits-map)] 
+    (part1 (reduce + (map :depth (vals with-depths))))
+    (part2 (- (get-in (distances-from orbits-map "YOU") ["SAN" :dist]) 2))))
index 2e58ab0491cd82481490bd3613dad2727e9ff6a2..9e8d2b57b2a50a94b3915b0a77453f0d36619db1 100644 (file)
@@ -1,6 +1,7 @@
 (ns adventofcode2019.day00
     [:require [adventofcode2019.lib :refer :all]
               [clojure.string :as str]
+              [clojure.core.match :refer [match]]
               [clojure.math.combinatorics :as combo]])
 
 (defn day00 []