Add correct day12pt2
authorJack Kinsey <j.jameskinsey@gmail.com>
Sun, 22 Dec 2019 02:25:54 +0000 (21:25 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Sun, 22 Dec 2019 02:25:54 +0000 (21:25 -0500)
project.clj
src/adventofcode2019/day12.clj

index cb2791c1e3cc8909a39ce1927d6943c130993674..da405733d89d9a1e3d4e6d70722ece75a961302a 100644 (file)
@@ -7,6 +7,7 @@
                  [org.clojure/core.match "0.3.0"]
                  [org.clojure/core.async "0.6.532"]
                  [org.clojure/math.combinatorics "0.1.6"]
+                 [org.clojure/math.numeric-tower "0.0.4"]
                  [org.clojure/data.priority-map "0.0.10"]
                  [clojure-lanterna "0.9.4"]
                  [aysylu/loom "1.0.2"]]
index d10e74b514cf7d427c7571a086cfc8f5d56b7ee0..acf3ff60024e989c585f889d1442222d90d3e58b 100644 (file)
@@ -4,7 +4,7 @@
    [clojure.string :as str]
    [clojure.core.match :refer [match]]
    [clojure.math.combinatorics :as combo]
-   [clojure.pprint :refer [pprint]]])
+   [clojure.math.numeric-tower :as math]])
 
 ; <x=1, y=2, z=3> -> [[1 2 3] [0 0 0]]
 (defn parse-coords [coords]
                               (assoc-in [j 1] nv2))))]
     (mapv velocity (reduce apply-gravity bodies all-pairs))))
 
-(defn find-cycle [states]
+(defn find-cycle [xs ys zs]
   (let [detect-dup (fn [[ct prev] state]
-                     (if (= (flatten prev)
-                            (map - (flatten state)))
-                       (reduced (inc ct))
-                       [(inc ct) state]))]
-    (* 2 (reduce detect-dup [0 []] states))))
+                       (if (prev state)
+                           (reduced ct)
+                           [(inc ct) (conj prev state)]))
+        dup-rep (partial reduce detect-dup [0 (hash-set)])]
+    (reduce math/lcm (map dup-rep [xs ys zs]))))
 
 (defn day12 []
   (let [input (mapv parse-coords (get-list-from-file (input-file)))
-        simulate (iterate step-simulation input)]
+        input-x (mapv (fn [[[p _ _] [v _ _]]] [[p] [v]]) input)
+        input-y (mapv (fn [[[_ p _] [_ v _]]] [[p] [v]]) input)
+        input-z (mapv (fn [[[_ _ p] [_ _ v]]] [[p] [v]]) input)
+        simulate (iterate step-simulation input)
+        simulate-x (iterate step-simulation input-x)
+        simulate-y (iterate step-simulation input-y)
+        simulate-z (iterate step-simulation input-z)]
     (part1 (reduce + (map total-energy (nth simulate 1000))))
-    (part2 (find-cycle simulate))))
+    (part2 (find-cycle simulate-x simulate-y simulate-z))))