doing Advent of Code hardcore Kotlin mode

So like many of you know, Advent of Code is happening right now, and I couldn’t resist a dopamine hit we get just from writing pure code but with a twist: writing it in fully immutable and functional idiomatic Kotlin style. I heard someone on Reddit is trying the same so I probably not alone. So please roast my code

No modifiable collections (including stacks, queues, maps, sets etc), no var s, not even recursive functions or for loops (even though technically it would be ok). No side effects. I didn’t use ChatGPT and turned off Copilot.

Few observations after doing Part 1 and Part 2 tasks of first 5 days:

  • It feels like it’s less buggy process comparing to if I were to use mutable data structures code. I think it’s because it’s easier to reason about data that is free from side effects and changes.
  • Plenty of fold and reduce calls. I feel like they’re good substitute for code that would usually require a modifiable collection or var.
  • Generally harder to read code with “it” variable everywhere. It is on me though since I wasn’t strict about maintainability of the code (obviously this code is “fire and forget”)
  • It’s harder to reason about complex types. IDE helps with type hints but it would be better to be more explicit about types. Good thing Kotlin allows that. In one case I got so much confused with multiple nested Lists and Pairs that I had to use typealias.
  • Even though I would expect code to be heavy on memory, no performance issues using immutable data structures. Also, I would suspect it’s east to parallelize and distribute.
  • Part 1 tasks are easy, and I clear them away in the mornings so I can read Part 2 problem which is the real one. Then it brews in my brain on background thread during the day, so I just type it up in the evening. A twist in Part 2 usually requires new approach.
  • Advent site gives points for speed. I’m too old for that: I see people solving the problems in under a minute according to leaderboard! That’s crazy fast, Rain Man level 🙂 Anyway, my goal was not the speed.

Overall, after 5 days worths of tasks, it feels like pretty much everything is double without modifiable collections. I’ll try to use more of that in my code in my projects.

Anything else I can try there to make it even more Kotlin-ish?

​

submitted by /u/Andriyo
[link] [comments]