Back

My Advent of Code in Roc 2023

It starts like it always does. Everything is fun and games before they hit you with the algorithm-heavy days. And then you can’t muster up the strength to keep up anymore. And then you give up. But I still mostly scraped by until day 20 (skipping 14 and 17, which I keep telling myself I will get back to anytime before next December).

At least my language of choice gave me an interesting extra challenge of solving the task in a way that the compiler likes. I.e. doesn’t panic or crash without giving any output. — Jokes aside, as December went on I learned the tricks to work around the compiler bugs and crashes, and I’m sure the Roc language developers got a fair share of bugs to squash until next year.

Recursive mindset

Roc doesn’t even have iterative loop constructs like for or while, instead it relies on recursion, which can be a bit getting used to. But Roc promises the same benefits in terms of runtime performance and memory usage using a combination of different strategies — sometimes the compiler can just replace the recursive call with a loop, and in more advanced cases you are encouraged to use tail-recursion. The upside of being forced into thinking in terms of recursive algorithms is that they can sometimes be a bit easier to understand and implement when reading Wikipedia’s pseudocode.

Aside from recursion, I also learned a bunch of useful stuff, like the Shoelace formula, which calculates the total area of a polygon from the set of points, Karger’s algorithm, which can be used to “min-cut” a graph, and of course our old pal Dijkstra made some appearances.

What’s next?

Roc is implemented in part with Zig which piqued my interest and I would enjoy investigating it further, but in another project.

My month of Roc got me into making a (slow as molasses) Ray tracing renderer while following along the Ray tracing in one weekend e-book. Both The Cherno and Sebastian Lague were great resources along the way with great visualizations to help further my understanding. Maybe I should try making a renderer using Ray marching next?

I just finished my third week as an independent consultant, and next week I’m going skiing which right now feels like a much-needed break from everything.

Johan