Ruby
Why Ruby is the Best Language for Advent of Code
It's the most wonderful time of the year - Christmas Advent of Code time! Advent of Code is an Advent Calendar style series of programming puzzles put out each year, starting on December 1st leading up to Christmas. The puzzles are super festive and ramp up in difficulty over the course of the month. Programmers of every level can participate, and in researching some of the more difficult problems you'll probably learn something cool! It's a great way to finish out the year.
I've been taking part in Advent of Code since 2019 (I've never completed a full year - and that's ok! You can participate for as long as it's fun and have the time) and have tried solving in multiple different languages - Advent is a great way to learn/skill up in a new language. But Ruby remains my favorite language in which to solve these puzzles.
Many of Ruby's strengths - its flexibility, robust standard library, and tooling make it the ideal language for Advent of Code.
Flexibility
Ruby doesn't enforce any one way of writing code. Want to solve a problem with a procedural script? Go for it! Want to leverage object-oriented programming and send messages between classes? Can do! Want to write in a functional style and map and zip a data structure in one long chain? You can do that too! And you can mix and match paradigms between problems - whatever models each problem best.
Ruby's data structures are super flexible as well. In many of the problems, Array and Hash allow you to very quickly model solutions. But if you find a hash isn't quite cutting it and you don't want to upgrade it to a full class, you can use the Data class to create value objects. This will lend you a bit more structure than a hash, and allow you to encapsulate some logic inside it without having to bring in the overhead of a Class.
Some of Ruby's flexibility - metaprogramming in particular - can be...unpopular... in larger production codebases. It's the "magic" that can lead to some nasty bugs. But Advent is a great place to play with those features. So go ahead, open up String and redefine some methods! Live life dangerously!
Standard Library
The Ruby standard library is well-suited for Advent of Code.
Array and Hash will probably be your best friends, and can get you pretty far in Advent. Because they both implement Enumerable, you can easily slice, dice, and transform these data structures with higher order functions like map, reduce, zip, and each. Convenience methods like sum and chunk mean you don't have to reinvent the wheel over and over again.
Set operations are built in - union (|), intersection (&), difference (-), and XOR (^). I've had to define these in other languages and while not difficult, it takes away from the actual solving of the puzzle.
The built in matrix gem can be a nicer way to represent grids than using arrays of arrays. It also includes some utilities to transpose, inverse and check if symmetric or upper or lower triangular.
The prime gem (included in Ruby) has utilities for listing prime numbers, determining if a number is prime, and decomposing integers into their prime factorization. It's up to you if you want to write this kind of thing yourself or skip ahead to the more exciting parts of the puzzle.
Tooling
Ruby's tooling makes it very easy to run, debug, and test your Advent solutions. Drop into an irb session, load in your code and play around with it. Within the REPL you can run the whole solution, or poke around at different methods to see if they're behaving as expected. Use debug or pry to pause execution and check the current state of your variables. When you're exploring solutions and not quite sure where to go or what's going wrong, a REPL is a great way to think, try things out, and iterate.
You can also write tests for Advent of Code! (I do!) RSpec or Minitest are easy to add into a project. You can write unit tests for various helper methods. And you can write tests for each solution's output - great if you want to refactor or try out a performance optimization and ensure you still get the same solution.
Summing it up
Sure, other languages have some or all of these features. But I don't know of any other language that has them all and is as pleasant to use as in Ruby.
Ruby was designed for programmer happiness. So wouldn't that make it the best choice for a fun programming challenge? The two were made for each other.
Photo by Jocelyn Allen on Unsplash