X-Git-Url: http://git.jkinsey.net/?p=adventofcode2019.git;a=blobdiff_plain;f=src%2Fadventofcode2019%2Fday12.clj;fp=src%2Fadventofcode2019%2Fday12.clj;h=d10e74b514cf7d427c7571a086cfc8f5d56b7ee0;hp=7326e425a72c8d73c9759ee06ef7c8ef480e0d8e;hb=0449019578dbfc4c0b280578cb9e48d5bb1e1079;hpb=fa02619ed39b05a9d8bb83e73aac4779dd38cac5 diff --git a/src/adventofcode2019/day12.clj b/src/adventofcode2019/day12.clj index 7326e42..d10e74b 100644 --- a/src/adventofcode2019/day12.clj +++ b/src/adventofcode2019/day12.clj @@ -1,10 +1,10 @@ (ns adventofcode2019.day12 - [:require [adventofcode2019.lib :refer :all] - [adventofcode2019.intcode :as i] - [clojure.string :as str] - [clojure.core.match :refer [match]] - [clojure.math.combinatorics :as combo] - [clojure.pprint :refer [pprint]]]) + [:require [adventofcode2019.lib :refer :all] + [adventofcode2019.intcode :as i] + [clojure.string :as str] + [clojure.core.match :refer [match]] + [clojure.math.combinatorics :as combo] + [clojure.pprint :refer [pprint]]]) ; -> [[1 2 3] [0 0 0]] (defn parse-coords [coords] @@ -41,8 +41,16 @@ (assoc-in [j 1] nv2))))] (mapv velocity (reduce apply-gravity bodies all-pairs)))) +(defn find-cycle [states] + (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)))) + (defn day12 [] (let [input (mapv parse-coords (get-list-from-file (input-file))) - simulate (iterate step-simulation input)] + simulate (iterate step-simulation input)] (part1 (reduce + (map total-energy (nth simulate 1000)))) - #_(part2))) + (part2 (find-cycle simulate))))