; Returns the number of orbits the given object is contained in (i.e. direct orbit + indirect orbits)(defnorbit-centers[orbit-mapobject](count(take-whilesome?(drop1(iterateorbit-mapobject))))); Returns the sum of the orbit centers for each object in the orbit map.(defndirect-and-indirect-orbits[orbit-map](apply+(map(partialorbit-centersorbit-map)(keysorbit-map)))); Parses the input format into a map of object -> orbit center(defnparse-orbit-map[raw](applyhash-map(flatten(map(compreverse#(.split#"\)"%))(.split#"\n"raw)))))(definput(parse-orbit-map(slurp(first*command-line-args*))))(println"Total number of (in)direct orbits:"(direct-and-indirect-orbitsinput))
I kinda got stuck on part 2, so the solution for this isn't really optimal (takes around half a second):
; Returns a sequence of objects that orbit the given center(defnorbiting[orbit-mapcenter](mapfirst(filter#(=(%1)center)orbit-map))); Calculates the minimum amount of traversals required in an orbit map to move from object to destination; (This is currently very inefficient and bad, I might still improve it)(defntraversals[orbit-mapobjectdestination](condp=objectnilInteger/MIN_VALUEdestination-2(inc(applymax(traversals(dissocorbit-mapobject)(orbit-mapobject)destination)(for[orbit-obj(orbiting(dissocorbit-mapobject)object)](traversals(dissocorbit-maporbit-obj)orbit-objdestination))))))(println"Traversals required to get from you to santa:"(traversalsinput"YOU""SAN"))
; Counts the amount of elements in coll before element appears. Returns (count coll) if it doesn't appear at all.(defncount-until[elementcoll](count(take-while(partialnot=element)coll))); Calculates the minimum amount of traversals required in an orbit map to move from object to destination(defntraversals[orbit-mapobjectdestination](let[object-centers(orbit-centersorbit-mapobject)destination-centers(orbit-centersorbit-mapdestination)first-common-center(some(setdestination-centers)object-centers)](+(count-untilfirst-common-centerobject-centers)(count-untilfirst-common-centerdestination-centers))))
I just fetch the first common center of both objects and add the steps it takes to get there for both.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Part 1 was a no-brainer in clojure:
I kinda got stuck on part 2, so the solution for this isn't really optimal (takes around half a second):
Fun one nonetheless!
(Full code: github.com/jkoenig134/AdventOfCode...)
I am fake upset you didn't break out the clojure zipper library for this! (it's like using a cannon for flies in this case)
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.
I just fetch the first common center of both objects and add the steps it takes to get there for both.