adventofcode

Aleks 2022-12-17T15:54:55.592719Z

today’s easter egg https://music.youtube.com/watch?v=I8LnIY8rOCA

2022-12-17T07:43:13.346949Z

Day 17 - Solutions

nbardiuk 2022-12-17T12:25:22.659149Z

I also hard coded boundaries for cycle in part 2 https://github.com/nbardiuk/adventofcode/blob/master/2022/src/day17.clj

Miķelis Vindavs 2022-12-17T16:46:20.578169Z

@nbardiuk what a beautiful solution — very readable & expressive! I like the use of reductions and cycle , and the reusing of dh in part 2

Miķelis Vindavs 2022-12-17T16:47:31.215819Z

I wasn’t able to get my clj solution for today to run faster than 3 or so seconds. Whereas my simpler python solution runs in 70ms 😕

Miķelis Vindavs 2022-12-17T16:50:53.643929Z

then again in py i used a bitmask to represent each line of the stack

nbardiuk 2022-12-17T16:53:37.018159Z

yes, my solution is slow. Also I track which point belongs to which original shape for colorful rendering

1
Miķelis Vindavs 2022-12-17T16:55:52.207029Z

my comment about the slowness wasn’t directed at you, it was about my own clojure solution 😅 sorry if it sounded that way yes, i noticed that you assoc the keyword for each shape into the map. What are you using to generate that image?

nbardiuk 2022-12-17T16:57:22.640399Z

I didn't took it personally, I meant my code is ALSO slow 🙂 I use clojure2d, the file with sketch is not that readable https://github.com/nbardiuk/adventofcode/blob/master/2022/sketch/day17_sketch.clj

Miķelis Vindavs 2022-12-17T16:57:53.781909Z

nice, i didn’t know about clojure2d

nbardiuk 2022-12-17T16:59:20.668619Z

it's great - runs on latest jvm (while quil doesn't) and has documentation site with examples https://clojure2d.github.io/clojure2d/docs/codox/index.html

2
Miķelis Vindavs 2022-12-17T17:00:13.444519Z

i also find it surprising that you can find the cycle just by looking at the height differences, that’s so much simpler than what i did — make a “fingerprint” of the last 10 rows, basically the relative max height of each x coordinate

Miķelis Vindavs 2022-12-17T17:01:48.769669Z

i don’t think either solution would work correctly for all possible inputs though e.g. mine definitely wouldn’t work if there were “caves” under an overhang

Miķelis Vindavs 2022-12-17T17:03:05.957819Z

or a super tall empty gap on one side which then later gets filled by dropping an I shape down more than 10 rows

nbardiuk 2022-12-17T17:03:54.752919Z

yes, last 10 rows are not stable because peaces can fall between cracks. Also heights can repeat while the shape does not.

Aleks 2022-12-17T17:11:06.886649Z

@mikelis.vindavs “to run faster than 3 or so seconds” Is it for part 1 or part 2?

Miķelis Vindavs 2022-12-17T17:11:16.873159Z

for part 2

Miķelis Vindavs 2022-12-17T17:14:19.223859Z

I store the occupied slots as a set of x,y tuples This means that collision tests, but especially fingerprinting (which has to do at least 1 linear search over the entire set to find the max Y coordinate), get slower as the stack gets filled up. What’s interesting is that i tried pruning down the set of coordinates every once in a while, but that only made the whole thing even slower

Aleks 2022-12-17T17:16:58.996169Z

Interesting, pruning down the set was my first thought when I saw part 2 question.

Miķelis Vindavs 2022-12-17T17:17:50.626469Z

the cycle occurs pretty fast so it doesn’t get that huge- it always stays below 10k elements

Miķelis Vindavs 2022-12-17T17:25:20.006819Z

i just realized i can pass in the “max y” from outside, now it all takes 100ms

🔥 1
Miķelis Vindavs 2022-12-17T17:26:09.028359Z

that’s kind of funny

Aleks 2022-12-17T17:27:30.865839Z

I ended with this, works relatively fast

(defn reify-rock [state]
  (let [{:keys [rock chamber position height]} state
        [cx cy] position
        [chamber' height'] (reduce (fn [[m h] [x y]]
                                     (let [x' (+ x cx)
                                           y' (+ y cy)]
                                       [(assoc m [x' y'] \#) (max h y')]))
                                   [chamber height]
                                   rock)]
    (assoc state
           :chamber chamber'
           :height height')))

Miķelis Vindavs 2022-12-17T17:30:42.355799Z

what’s position, is it the bottom left corner of the rock?

Miķelis Vindavs 2022-12-17T17:31:07.984559Z

and how do you detect cycles for p2?

Aleks 2022-12-17T17:31:13.742479Z

Yes, it is

Aleks 2022-12-17T17:32:30.847999Z

For part 2 I did it manually with a notepad 🙃

📝 1
😅 1
Miķelis Vindavs 2022-12-17T17:33:01.302119Z

nice

Miķelis Vindavs 2022-12-17T17:34:33.806019Z

btw, another finding that was surprising to me

;;(def intersects? (comp seq set/intersection))

(defn intersects? [a b]
  (if (< (count a) (count b))
    (some b a)
    (some a b)))
Doing this (avoiding construction of the overlapping set, instead breaking early if anything overlaps) didn’t change the run time much

Miķelis Vindavs 2022-12-17T17:34:58.828139Z

in general, i keep getting surprised by how ok it is to generate lots of garbage

2022-12-17T20:39:45.416729Z

I enjoyed this one, was able to get something that solves part-2 in about a second: https://github.clerk.garden/alexalemi/clerktudes/commit/2da0d9e6008e02078e524bf6df368a410e11f132/notebooks/advent-2022-17.html

2022-12-17T07:53:40.159829Z

https://gitlab.com/maximoburrito/advent2022/-/blob/main/src/day17/main.clj Part 1 was very fun, though it took much longer than I thought. Part 2 was really tough, but we've had cycle detection problems before so it seemed relatively clear what generally needed to be done. Part2 here is kind of ugly. I dumped my height differences into emacs and played with the buffer a bit to confirm there were cycles and what size they were to write a more targeted solution. It's not my finest clojure, but it works...