This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # adventofcode (20)
- # aleph (25)
- # announcements (4)
- # babashka (117)
- # beginners (150)
- # calva (4)
- # cider (9)
- # clj-on-windows (2)
- # clojure (9)
- # clojure-europe (4)
- # clojure-italy (3)
- # clojuredesign-podcast (18)
- # clojurescript (16)
- # conjure (1)
- # core-async (35)
- # cursive (13)
- # datalevin (6)
- # datomic (6)
- # fulcro (8)
- # hyperfiddle (5)
- # malli (8)
- # nextjournal (4)
- # off-topic (51)
- # pathom (4)
- # reagent (21)
- # sci (14)
- # shadow-cljs (22)
- # specter (3)
- # testing (22)
- # tools-deps (8)
- # xtdb (7)
solved with zippers 🙂 https://gist.github.com/erdos/797f5a63016b919e43d619fb3a957b49
https://gitlab.com/maximoburrito/advent2021/-/blob/main/src/day18/main.clj I also went with zippers, despite the fact that in my 8 or 9 years with Clojure I've never had cause to use them before. I almost gave up to switch to a mutable tree structure, but at the last second I found the problem I was having.
flattened tree into vector of pairs of value and path https://github.com/nbardiuk/adventofcode/blob/master/2021/src/day18.clj
took this opportunity to understand zippers deeper https://github.com/zelark/AoC-2021/blob/main/src/zelark/aoc_2021/day_18.clj
Thank you for suggestion on zippers https://github.com/jacoelho/advent-of-code-clj/blob/main/src/aoc/2021/day18.clj Part 2 takes like 8seconds, is that expected or is an implementation issue?
https://github.com/kconner/advent-of-code/blob/master/2021/18a.clj, https://github.com/kconner/advent-of-code/blob/master/2021/18b.clj. 1.2s and 3.3s. I did think this might be the kind of thing a zipper is good for, but i didn't research and use them this time. maybe I'll be inspired next time by having done it the hard way already 🙂
if you treat the index path to the node to be exploded as a four-bit binary number, you can find the leaf to either side by incrementing or decrementing that path number. if you overflow, you're out of the structure. then extend with a fifth bit for depth, and find the leaf on that path.
no zippers here, but it looks like it would be a good problem to revisit to give them a try…
only interesting bit is
explode, which walks the tree depth first and returns a triple of “left carry”, “exploded number”, “right carry”. which get carried back up the call stack and applied as appropriate.
I had the feeling that something like zippers/lenses or other similar things I never learned about would be applicable, but I didn't have the time to look into them now. maybe my solution is somehow equivalent? anyway, it ended up shorter than the zipper-based ones, without any
when I read @U076FM90B's description I thought "yes, that's exactly what I did too!", but somehow the actual code seems quite different. my main trick was "make a sequence of the paths to the leaves, then partition that to get the previous and next path for explodes"
Mine was nothing fancy; I just used a whole lot of
get-in and similar basic vector functions. Still, it all worked out fairly simply.
I went with a flattened list where each number is a pair with value and nesting level
https://github.com/kfirmanty/advent-of-code-2021/blob/main/src/day18.clj - this took way longer that I have should probably due to me being sick and not sleeping well 😄 Went with zippers and stick with them but would lie if said I know them well beforehand so had to do a lot of learning while solving it. It is nice that I have purely functional solution that mutates a tree but TBH if I would need to do such task more often I would just use some mutable data structure and be done with it 😄
I used zippers, fun stuff. Although slow around 18 seconds for part 2 initially, but brought it under 7 secs using