Learning Julia with Advent of Code

Advent of Code is a great way to learn a new programming language.
Julia
Published

January 24, 2022

Why Julia?

Around November of 2021, I decided I wanted to learn how to program in Julia. Why Julia?

Julia is a high-level, dynamic programming language, designed to give users the speed of C/C++ while remaining as easy to use as Python. This means that developers can solve problems faster and more effectively.1

What is Advent of Code?

But the best way to learn a programming language is to actually write code. But what to code? It’s boring to just re-write things that you’ve already done. Enter Advent of Code:

Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

The puzzles are whimsical and a lot of fun, and I was challenged to learn a bunch of new data structures and algorithms. This YouTube video was a nice introduction: Jumping into the Julia Community via Advent Of Code | Jasmine Hughes | JuliaCon2021

Other collections of puzzles include Rosalind, which are mostly related to bioinformatics but also general computer science algorithms, and Project Euler, which feels more math-y. They didn’t seem like much fun, not involving helping Santa’s elves or strange amphipod creatures (these creatures helped me learn the A-star algorithm). CodinGame looks like it’d be a lot of fun, but doesn’t support Julia :(

Being humbled…

The Julia community was quite welcoming and it was fun to solve the puzzles at the same times others were. The Julia Zulip forum was a great place to ask for help and see how others were approaching the puzzles.

After solving a puzzle on my own and being pleased with my solution, I’d sometimes post it to the Zulip, and then looked at some of their solutions. This was… always very humbling… But it’s a great way to learn.

Over the next 2 or 3 months, I solved the Advent of Code puzzles for both 2021 and 2015, sharing them in realtime to GitHub repositories /jhchou/2021_adventofcode and /jhchou/2015_adventofcode. If you look at this code, please remember that I’m not a “real” programmer (no formal training) and I was completely new to the language.

Incidentally, here’s a handy snippet of Julia code to download all the puzzle inputs for multiple years of Advent of Code (you’ll need to extract your session_cookie):

using Downloads: download

session_cookie = read("session_cookie.txt", String)

for year in 2015:2021
    path = joinpath(@__DIR__, "data", "$year")
    mkpath(path)
    for day in 1:25
        url = "https://adventofcode.com/$(year)/day/$(day)/input"
        data_file = joinpath(path, "day$(day).txt")
        println("Day $day: $data_file")
        download(url, data_file, headers = Dict("cookie" => "session=$(session_cookie)"))
        sleep(0.1)
    end
end

Conclusion

Solving Advent of Code puzzles was a great experience, and when I have the time, I’d like to do the rest of the years of puzzles.

As for Julia, it’s now my favorite “general purpose” programming language. I can’t explain why, but it’s just a pleasant language to code in. It’s just a fun and clean language. The major downsides for me are that if I have a data analysis project where I just want to get it done, it’s hard to justify not using R and tidyverse which I’m relatively fluent in, and instead doing things more slowly in Julia.

Although I do most of my “work” stuff in R, I find it feels very forced to try to do general purpose programming in R.

As for Python, again, I can’t explain why, but I just don’t like the feel of it. Maybe as an “older” language, it’s accumulated too many inconsistencies between different packages.

Hope to get a chance to dive back into Julia soon, and hopefully get better at it.