This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-12-06
Channels
- # adventofcode (99)
- # announcements (9)
- # aws (3)
- # babashka (22)
- # beginners (90)
- # boot (2)
- # calva (22)
- # cider (8)
- # clj-kondo (14)
- # cljsrn (20)
- # clojure (24)
- # clojure-europe (4)
- # clojure-italy (3)
- # clojure-losangeles (1)
- # clojure-nl (83)
- # clojure-spain (1)
- # clojure-spec (46)
- # clojure-uk (43)
- # clojuredesign-podcast (70)
- # clojurescript (40)
- # cursive (25)
- # datomic (9)
- # duct (3)
- # emacs (14)
- # figwheel-main (2)
- # fulcro (61)
- # graalvm (8)
- # juxt (7)
- # kaocha (2)
- # leiningen (19)
- # luminus (5)
- # malli (58)
- # off-topic (4)
- # re-frame (11)
- # reitit (5)
- # rewrite-clj (3)
- # shadow-cljs (63)
- # sql (5)
- # testing (5)
- # tools-deps (26)
- # uncomplicate (2)
- # vim (4)
https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2019/day5.clj
Yes I got invalid positions when my implementation was wrong 😉
Are you always interpreting the target parameter as the address value without looking up the value st the address? That was my error
I may actually expand my visualization to serve as a diagnostic panel if this machine continues expanding in the future.
Makes for easier debugging of issues like that if you can also expose intermediary steps, like the mode-interpreted values.
iterate
is just a great general purpose way to unroll a loop-recur
So much easier to inspect the result and compose it with other data
Really, for me, I reach for it because it’s easier to think about a single input/output than to think about flow control.
I think I resisted iterate
for day 5 because checking the terminate condition requires unpacking the step function a little
Er, breaking the pattern. Like you used ::halt
as your return value
Normally I like to write my iterate step function so it "could" run forever, and it's the consumer's decision when to stop it
I couldn't think up an obvious way to do that here so I went the fn-recur route
yeah you could return a fixed point
Rich Hickey is watching you code
haha, sometimes I wonder wtf happened when I peek into a core function's implementation
It's like the least lispy, most complicated thing internally
but outside it's a beautiful flower
can I not
I can respect the person who hands me a gun when I say I want to shoot myself in the foot
> Parameters that an instruction writes to will never be in immediate mode. this is the part I think my solution fails at
well, core should be fast so that userspace could afford to be readable
Day 6, was way easier than day 5 thank goodness. Part 1. Get the list of nodes by putting all the node names into a set, then follow the chain of orbits back to “COM” and sum all the chain lengths. Part 2. Find the first common node in the chains from “YOU” and “SAN” to back to “COM” and count the links to that common node.
part 1 what do you mean by putting all the node names into a set? I put them into a map to follow the chain of orbits back to COM part 2 I put both chain of orbits into a set, count the diff, add both counts minus diff
I replaced my transducers solution with a sets one, and it went from 1.5ms to 3.5ms :) https://github.com/akovantsev/adventofcode/commit/c3b234ecaacd2014d360c4e20ee946086e6b5e73
(defn set-symmetric-difference [s1 s2]
(set/difference (set/union s1 s2) (set/intersection s1 s2)))
(count (set-symmetric-difference p1 p2))
main thing is to make a a orbits b data structure not b is orbited by a as it is in de data
Today was easier than yesterday. Solution for both in 7 lines total: https://gitlab.com/dmarjenburgh/adventofcode/blob/master/src/adventofcode/year_2019.clj#L123-129
Elegant! Good call that you don't need to find the least common ancestor - any ancestor will do
I looked at this before solving it. I will try! 😄
Never used it
(def input
(->> (-> (io/resource "2019/day6.txt")
slurp
(string/replace #"\)" "\n")
(string/split-lines))
(map keyword)))
(def universe
(atom (make-hierarchy)))
(defn add-orbit! [p1 p2]
(swap! universe derive p1 p2))
(defn add-orbits! [input]
(doseq [[p1 p2] (partition 2 input)]
(add-orbit! p1 p2)))
(defn count-orbits [p]
(let [ps (parents @universe p)]
(if (zero? (count ps))
0
(apply + (count (ancestors @universe p)) (map count-orbits ps)))))
;; Part 1
(add-orbits! input)
(count-orbits :COM)
StackOverFlow error 😉The hierarchy doesn't need to be in an atom, just pass it around like a first-class citizen. You might have to forgo tree recursion as well to avoid the overflow. If you want to see how I solved it: https://github.com/uosl/advent-of-code/blob/master/src/aoc2019/06.clj
This works:
(defn count-orbits [planets]
(apply + (map (comp count (partial ancestors @universe)) planets)))
Where planets set of the planets
I’ll check your answer later puzzle around for 2 first 😉
Ah I changed it without the atom, just the global hierarchy 😉
Nice use of reduce
!
finished my day 6 https://gist.github.com/roman01la/eba8c713949f4e115c50488eecf0e3e8
Inspired by the make-hierarchy
comment of @regen I decided to misuse the global hierarchy: https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2019/day6.clj
you can do the last bit with just set operations.
(defn part-2 [input]
(let [orbits (make-hier (map parse-rel input))
ys (ancestors orbits :YOU)
ss (ancestors orbits :SAN)]
(count (set/difference (set/union ys ss) (set/intersection ys ss)))))
I see. Why does that work? 😛
Aaaahh
broke down and finally learned ubergraph this year https://github.com/potetm/advent-of-code/blob/master/src/advent_2019/day_6.clj
Did you look at @potetm? ubergraph
does even more 😛
(let [g (graph (parse s))]
(- (uga/cost-of-path (uga/shortest-path g "YOU" "SAN"))
2)))
it works pretty well with Clojure, but staying in purely idiomatic Clojure would be nice.
I did it just with a Map. I think it worked out okay <https://github.com/Average-user/adventofcode-clj-2019/blob/master/src/adventofcode_clj_2019/day06.clj>