AdventOfCode

All File in 2021/
Login

Files in directory /2021 in any check-in


Advent of Code 2021

This was my first year doing AoC and I, like many others, used it to become more familiar with a new language, in this case OCaml 4.13. Most of the code here subsequentally represents a learning adventure and isn't good, idiomatic or at all performant. You've been warned.

To get started, envrc is recommended to help out:

opam switch create . 4.13.1 --deps-only

Then for each day:

cd <day>
dune exec ./solutions.exe part1 ./input
dune exec ./solutions.exe part2 ./input

Other utils:

dune runtest --watch
dune build @fmt --auto-promote

There is a common Lib module which holds a few utils that I've used a number of times while solving the puzzles, as well as a parser combinator library that I ended up writing starting around day 16 after getting tired of regex parsing the inputs.

Each day is organized into three parts

The solutions.ml which is boilerplate around the days solutions and the entry point into the solutions using the cli pattern: solutions.exe part(0|1) ./input:

open Printf
open DayXX

let () =
  let part = Array.get Sys.argv 1 in
  let filename = Array.get Sys.argv 2 in
  let lines = Lib.read_lines filename in
  match part with
  | "part1" -> printf "part 1\n"
  | "part2" -> printf "part 2\n"
  | cmd ->
      printf "unknown part `%s'!" cmd;
      exit 1

Tests are under tests and use OUnit2 around the puzzle examples to provide a TDD driven experience.

The solution code for the day lives under lib and is organized into common.ml components that are applicable to both parts, as well as parts specific functionality in their respective file.

To note: I've tried to avoid using any external dependencies beyond my own common library code as an excercise around exploring the base language and becoming familiar with the quirks and why projects like Base/Core and Batteries exist.