And here’s day 2. This time with regular Clojure (e.g. run with clj)
I tried to use my Ergodox for this so it took a long time and I did the debugging with my regular keyboard cause there’s a limit to how much frustration I can take
Too bad that as a European it’s basically impossible to take place in the ranking. Only the first 100 people get points, I did the exercise in the morning and I’m 3200th…
I came up with a more “mathematical” solution for day 3 part 1, rather than actually creating the full matrix, but this didn’t help for doing part 2 at all
Exactly what happened to me! I was happy that I figured out a “rule”-based approach for part 1. Then in part 2 I had to actually create the spiral array
I didn’t avoid using loop-recur which I know isn’t considered idiomatic Clojure. I’m not sure how I would avoid it in this case though. I’m also studying SICP at the moment so it has definitely influenced me!
Hi @javazquez, I’ve hidden your code so people can avoid seeing spoilers. You can do the same by clicking on the little gear icon in the editor and choosing “Hide details”.
I liked this one, I think the Clojure solution is pretty elegant. It’s certainly a good example of using recur, I wouldn’t know how to do this one without it.
When solving the second part I was worried for a bit I had ended up with infinite recursion cause it took quite some time to finish, about 21.5 seconds on my machine. It is performing 28 million steps but still. My guess is that’s the price you pay for constantly updating vectors. That’s a lot of HMAC nodes that are being created and consequently garbage collected.
ok, seems I was very wrong there. I tried to make a version that uses Java arrays, and it’s an order of magnitude slower. Clojure data structures for the win!
Final update for today The Java array version was about 38x times slower than the Clojure vector version, which I found surprising, so I tried if I could speed it up.
I split a few things up into helper functions so I could type hint them, and used Java interop instead of aget and aset, all to try and prevent it from having to box and unbox numbers, and from having to reflect on types.
This worked, now the Java array version is about 3x faster than the Clojure version, so about 100x faster than the Java version without type hints.
That’s very likely because you used reflection. (aget ...) is implemented depending on the array type. If CLJ doesn’t know which array you’re dealing with it’ll use reflection (Turn on reflection). You can avoid that by type hinting with ^longs: