https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2019/day5.clj
I’m getting a position of -80
in my input
did anyone else encounter this?
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.
Hmm. You could even time-travel it by storing a stack of state-diffs...
https://github.com/potetm/advent-of-code/blob/master/src/advent_2019/day_5.clj
ugh gotta stop checking in my puzzle inputs
somehow mine’s the first non-loop-recur solution I’ve seen
ah just saw uosl’s
basically the exact same approach
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
use iterate, pull out all your immediate/position vals, and case
it
yeah.. well
inspecting output wasn’t super duper for this problem
big long list of numbers
but ordinarily, yeah
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
unpacking?
Er, breaking the pattern. Like you used ::halt
as your return value
right, sig term
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
yeah I considered returning the same input
I couldn't think up an obvious way to do that here so I went the fn-recur route
then I was like, “What are you some kind of zealot?”
yeah you could return a fixed point
lol
what nobody sees don’t hurt em I figure
Rich Hickey is watching you code
iow implementation detail
you ain’t read a lot of rich’s code
apparently
😄
haha, sometimes I wonder wtf happened when I peek into a core function's implementation
dude do it
I do all the time
It's like the least lispy, most complicated thing internally
but outside it's a beautiful flower
yeah, consider volatile
can I not
DO YOU WANT SIMPLE OR COMPLECTED???
really, it’s, like, a pragmatists view
“Simple is nice. Working is better.”
I can respect the person who hands me a gun when I say I want to shoot myself in the foot
exactly
how very… American
ugh, I only just finished day 5
and day 6 is out in 45min
> Parameters that an instruction writes to will never be in immediate mode. this is the part I think my solution fails at
what is that supposed to mean?
you always write to the position that the param says
you never dereference the param and then write
where write means “update the program state”
Well day 6 problem description got vague at the end
visuals made it instantly obvious what's required, to me.
GraphViz to the rescue!
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
Try set/intersection of p1 and p2 instead then sum of both counts - 2 diff
nah, not as readable
(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 can't be the only one who found make-hierarchy
perfect for day 6?
That iterate with a map as the first argument is awesome
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
finished my day 6 https://gist.github.com/roman01la/eba8c713949f4e115c50488eecf0e3e8
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 😉
Good luck [=
Ah I changed it without the atom, just the global hierarchy 😉
Hehe
Nice use of reduce
!
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
nice, that is a clever hack
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)))))
(same make-hierarchy as the last few, i did not know about that core function!)
I see. Why does that work? 😛
Aaaahh
nice
broke down and finally learned ubergraph this year https://github.com/potetm/advent-of-code/blob/master/src/advent_2019/day_6.clj
Nice
Damn didn't even know make-hierarchy
existed. That basically did all the work lol
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)))
I may have to see about swapping my usual JGraphT dependency for Ubergraph...
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>