re: Advent of Code 2019 Solution Megathread - Day 6: Universal Orbit Map VIEW POST

re: Oh, I will definitely look into that, thanks for the suggestion. I only knew about (tree-seq), I'm still a newbie in clj.

Zipper is extremely powerful for navigating trees efficiently.

It’s also as easy to read as overly point free Haskell.

I vastly improved it without using zippers now.

; Counts the amount of elements in coll before element appears. Returns (count coll) if it doesn't appear at all.
(defn count-until [element coll]
  (count (take-while (partial not= element) coll)))

; Calculates the minimum amount of traversals required in an orbit map to move from object to destination
(defn traversals [orbit-map object destination]
  (let [object-centers (orbit-centers orbit-map object)
        destination-centers (orbit-centers orbit-map destination)
        first-common-center (some (set destination-centers) object-centers)]
    (+ (count-until first-common-center object-centers)
       (count-until first-common-center destination-centers))))

I just fetch the first common center of both objects and add the steps it takes to get there for both.

Code of Conduct Report abuse