adventofcode

Happy Advent 2020! Please put answers in the pinned threads or create one if it does not exist yet. | https://github.com/adventofcode-clojurians/adventofcode-clojurians | Join the private leaderboard with code 217019-4a55b8eb
lilactown 2019-12-06T02:11:40.324900Z

I’m getting a position of -80 in my input

lilactown 2019-12-06T02:11:46.325100Z

did anyone else encounter this?

mpcjanssen 2019-12-06T02:35:40.326400Z

Yes I got invalid positions when my implementation was wrong 😉

mpcjanssen 2019-12-06T02:39:24.327800Z

Are you always interpreting the target parameter as the address value without looking up the value st the address? That was my error

fellshard 2019-12-06T03:31:08.328500Z

I may actually expand my visualization to serve as a diagnostic panel if this machine continues expanding in the future.

fellshard 2019-12-06T03:31:56.329400Z

Makes for easier debugging of issues like that if you can also expose intermediary steps, like the mode-interpreted values.

fellshard 2019-12-06T03:37:11.329900Z

Hmm. You could even time-travel it by storing a stack of state-diffs...

2019-12-06T04:00:56.330600Z

ugh gotta stop checking in my puzzle inputs

2019-12-06T04:02:14.331100Z

somehow mine’s the first non-loop-recur solution I’ve seen

2019-12-06T04:04:25.331400Z

ah just saw uosl’s

2019-12-06T04:05:27.331900Z

basically the exact same approach

2019-12-06T04:06:12.332600Z

iterate is just a great general purpose way to unroll a loop-recur

2019-12-06T04:06:29.333100Z

So much easier to inspect the result and compose it with other data

2019-12-06T04:06:42.333500Z

use iterate, pull out all your immediate/position vals, and case it

2019-12-06T04:07:04.333700Z

yeah.. well

2019-12-06T04:07:13.334100Z

inspecting output wasn’t super duper for this problem

2019-12-06T04:07:17.334300Z

big long list of numbers

2019-12-06T04:07:36.334600Z

but ordinarily, yeah

2019-12-06T04:08:22.335800Z

Really, for me, I reach for it because it’s easier to think about a single input/output than to think about flow control.

2019-12-06T04:08:36.336Z

I think I resisted iterate for day 5 because checking the terminate condition requires unpacking the step function a little

2019-12-06T04:08:46.336200Z

unpacking?

2019-12-06T04:09:18.336700Z

Er, breaking the pattern. Like you used ::halt as your return value

2019-12-06T04:09:30.337200Z

right, sig term

2019-12-06T04:09:50.337700Z

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

2019-12-06T04:10:16.338300Z

yeah I considered returning the same input

2019-12-06T04:10:18.338600Z

I couldn't think up an obvious way to do that here so I went the fn-recur route

2019-12-06T04:10:26.338900Z

then I was like, “What are you some kind of zealot?”

2019-12-06T04:10:27.339Z

yeah you could return a fixed point

2019-12-06T04:10:30.339200Z

lol

2019-12-06T04:11:03.339700Z

what nobody sees don’t hurt em I figure

2019-12-06T04:11:16.340300Z

Rich Hickey is watching you code

2019-12-06T04:11:20.340400Z

iow implementation detail

2019-12-06T04:11:27.340700Z

you ain’t read a lot of rich’s code

2019-12-06T04:11:28.340900Z

apparently

2019-12-06T04:11:28.341100Z

😄

2019-12-06T04:12:04.341400Z

haha, sometimes I wonder wtf happened when I peek into a core function's implementation

2019-12-06T04:12:25.341800Z

dude do it

2019-12-06T04:12:29.342Z

I do all the time

2019-12-06T04:12:37.342200Z

It's like the least lispy, most complicated thing internally

2019-12-06T04:12:41.342400Z

but outside it's a beautiful flower

2019-12-06T04:12:54.342600Z

yeah, consider volatile

2019-12-06T04:13:02.342900Z

can I not

2019-12-06T04:13:35.343700Z

DO YOU WANT SIMPLE OR COMPLECTED???

2019-12-06T04:14:11.344200Z

really, it’s, like, a pragmatists view

2019-12-06T04:14:19.344600Z

“Simple is nice. Working is better.”

2019-12-06T04:14:28.344900Z

I can respect the person who hands me a gun when I say I want to shoot myself in the foot

1😀
2019-12-06T04:14:43.345100Z

exactly

2019-12-06T04:14:55.345400Z

how very… American

2019-12-06T04:15:29.345700Z

ugh, I only just finished day 5

2019-12-06T04:15:34.345900Z

and day 6 is out in 45min

1
lilactown 2019-12-06T05:06:11.346300Z

> Parameters that an instruction writes to will never be in immediate mode. this is the part I think my solution fails at

lilactown 2019-12-06T05:07:09.346600Z

what is that supposed to mean?

1
2019-12-06T05:11:06.347Z

you always write to the position that the param says

2019-12-06T05:11:58.347500Z

you never dereference the param and then write

2019-12-06T05:12:17.347800Z

where write means “update the program state”

kenj 2019-12-06T05:19:14.348700Z

Well day 6 problem description got vague at the end

misha 2019-12-06T06:18:17.349900Z

visuals made it instantly obvious what's required, to me.

fellshard 2019-12-06T06:33:01.350Z

GraphViz to the rescue!

2
mchampine 2019-12-06T07:09:28.355700Z

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.

yenda 2019-12-06T08:10:36.359500Z

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

misha 2019-12-06T08:29:02.359700Z

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

1😄
yenda 2019-12-06T10:49:10.362600Z

Try set/intersection of p1 and p2 instead then sum of both counts - 2 diff

misha 2019-12-06T11:10:58.363300Z

nah, not as readable

2019-12-06T16:49:48.372Z

(defn set-symmetric-difference [s1 s2]
  (set/difference (set/union s1 s2) (set/intersection s1 s2)))

(count (set-symmetric-difference p1 p2))

mpcjanssen 2019-12-06T07:31:06.357800Z

main thing is to make a a orbits b data structure not b is orbited by a as it is in de data

yenda 2019-12-06T08:10:36.359500Z

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

misha 2019-12-06T08:29:02.359700Z

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

1😄
2019-12-06T08:41:44.360500Z

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

fellshard 2019-12-06T09:26:00.361200Z

Elegant! Good call that you don't need to find the least common ancestor - any ancestor will do

uosl 2019-12-06T09:43:53.361600Z

I can't be the only one who found make-hierarchy perfect for day 6?

uosl 2019-12-06T09:50:22.361700Z

That iterate with a map as the first argument is awesome

2➕
yenda 2019-12-06T10:49:10.362600Z

Try set/intersection of p1 and p2 instead then sum of both counts - 2 diff

erwinrooijakkers 2019-12-06T10:49:17.362800Z

I looked at this before solving it. I will try! 😄

erwinrooijakkers 2019-12-06T10:49:29.363100Z

Never used it

misha 2019-12-06T11:10:58.363300Z

nah, not as readable

erwinrooijakkers 2019-12-06T11:45:11.363500Z

(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 😉

uosl 2019-12-06T11:49:35.363700Z

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

2019-12-06T11:50:52.364100Z

finished my day 6 https://gist.github.com/roman01la/eba8c713949f4e115c50488eecf0e3e8

erwinrooijakkers 2019-12-06T11:51:04.364200Z

This works:

erwinrooijakkers 2019-12-06T11:51:16.364400Z

(defn count-orbits [planets]
  (apply + (map (comp count (partial ancestors @universe)) planets)))

erwinrooijakkers 2019-12-06T11:51:44.364600Z

Where planets set of the planets

erwinrooijakkers 2019-12-06T11:52:31.364800Z

I’ll check your answer later puzzle around for 2 first 😉

uosl 2019-12-06T11:53:55.365Z

Good luck [=

erwinrooijakkers 2019-12-06T11:59:14.365200Z

Ah I changed it without the atom, just the global hierarchy 😉

erwinrooijakkers 2019-12-06T11:59:34.365400Z

Hehe

erwinrooijakkers 2019-12-06T12:27:37.365600Z

Nice use of reduce!

erwinrooijakkers 2019-12-06T12:41:07.368600Z

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

1😅
2019-12-06T12:45:17.369Z

nice, that is a clever hack

tws 2019-12-06T13:18:04.369800Z

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)))))

tws 2019-12-06T13:18:56.370200Z

(same make-hierarchy as the last few, i did not know about that core function!)

erwinrooijakkers 2019-12-06T15:21:46.370700Z

I see. Why does that work? 😛

erwinrooijakkers 2019-12-06T15:30:16.370900Z

Aaaahh

erwinrooijakkers 2019-12-06T15:30:18.371100Z

nice

2019-12-06T16:26:00.371500Z

broke down and finally learned ubergraph this year https://github.com/potetm/advent-of-code/blob/master/src/advent_2019/day_6.clj

erwinrooijakkers 2019-12-06T16:29:55.371800Z

Nice

2019-12-06T16:49:48.372Z

(defn set-symmetric-difference [s1 s2]
  (set/difference (set/union s1 s2) (set/intersection s1 s2)))

(count (set-symmetric-difference p1 p2))

Mario C. 2019-12-06T19:53:30.372900Z

Damn didn't even know make-hierarchy existed. That basically did all the work lol

erwinrooijakkers 2019-12-06T20:41:58.373600Z

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)))

fellshard 2019-12-06T22:21:07.374100Z

I may have to see about swapping my usual JGraphT dependency for Ubergraph...

fellshard 2019-12-06T22:21:29.374700Z

it works pretty well with Clojure, but staying in purely idiomatic Clojure would be nice.

Average-user 2019-12-06T22:27:17.376Z

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>