Day 21 — Solutions 😵💫
Well that was hard! Part 2 is solved I have pages of doodles and drawings that I used to figure it out. https://github.com/bhauman/adv2023/blob/main/src/adv2023/day21/sol.clj
I’m glad other folks struggled too. I didn’t want to look and see that people had done this in 5 minutes 🙂
Uhhh! Finally I nailed it. My code is dirty, I’m not sure if I want to post it at all. I believe it’s the toughest day so far
https://github.com/rjray/advent-2023-clojure/blob/master/src/advent_of_code/day21.clj I finished last night, after (shamefully) just translating a Python solution to Clojure. As if to taunt me, the Clojure takes 2x time to run that the Python did. I look forward to seeing other solutions, because I want to understand it better than I do from just cribbing Python. 😞. (Part 1 just took me ~30 min to solve, and about 1.3s to run.)
Part 1 is pretty simple, not fast but a simple solution:
(defn reached-plots [adjecent start limit]
(->> (iterate #(set (mapcat adjecent %)) [start])
(drop limit)
(first)))I'm still at part two. First part was memoized recursive function in my case. But I like above solution, which is much simpler.
I figured out a trick. My https://github.com/wevre/advent-of-code/blob/master/src/advent_of_code/2023/day_21_steps.clj is super slick.
Actually, it’s not “slick”, it’s very hacky. I have a lot of “magic numbers” that came from examining my input. But I suspect that it will work generally for any input.
My notes.
There is a parity to the garden plots. Once they are filled up, they blink on and off, from odd to even, like a checkerboard. Also for my input (and I suspect it is true for all the inputs) there is a clear, unobstructed path directly north/south/east/west from S. So for a board of size m, in N steps we’ll reach (/ (- N (/ m 2) m) plots away from the center. My input plots were 131x131, so 26502365 steps reached (/ (- 26502365 65) 131) ;;=> 202300 plots in each direction. All the plots within a (manhattan) distance of 202299 were completely full, just had to deal with the partial plots on the outer edge of the diamond.
Calculated the number for a couple of board widths (relying on the fact that each generation’s number is equal to the number for the generation before the last + the number of new positions taking steps from the last ‘frontier’ that were not in the ‘frontier’ before that) and then derived the corresponding quadratic formula (not programming, so does feel like cheating) which gave me the solution.