Fork me on GitHub

🧵Day 18 Solutions Thread: post your solution here

norman07:12:24 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.

👍 2

flattened tree into vector of pairs of value and path

👏 2

zippers look cool :the_horns:


Thank you for suggestion on zippers Part 2 takes like 8seconds, is that expected or is an implementation issue?

kevinc18:12:39 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.

👌 1
Callum Oakley21:12:03

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. 21ms/160ms


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 requires 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"

👍 1
Andrew Byala03:12:40

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. •

Antonio Bibiano08:12:29

I went with a flattened list where each number is a pair with value and nesting level

Antonio Bibiano08:12:26

and then used two queues and a buffer to scan the list

Antonio Bibiano08:12:07

took me a lot longer than I anticipated after coming up with the idea 😞

karol13:12:49 - 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 pmap


It’s almost dead silent today

☝️ 1

I am waiting for my kid to go asleep 😄 But took a quick glance at the task in the morning and my gut feeling would to use one inbuilt clojure "library" and the parsing of input looks to be most straightforward of all days (at least in clojure)

🙌 3