AOC (Advent of Code Utils v1.1.0) View Source

Advent of Code solution module macro and helpers.

This module contains the aoc/3 macro, which should be used to write a solution module for a given advent of code challenge. The intended use is to write your solution for day <day>, year <year> as follows:

import AOC

aoc <year>, <day> do
  def p1 do
    # Part 1 solution goes here
  end

  def p2 do
    # Part 1 solution goes here
  end
end

Defining a module with the aoc/3 macro has a few advantages:

  • Helper functions to access the input and examples (if present) are inserted into the generated module.
  • The AOC.IEx functions can be used to call your solutions in the module, making your life a bit easier.

Overall, the aoc/3 macro is intended to allow you to forego writing boilerplate code which is shared between all the solutions.

Note that the code skeleton shown above can be generated by running mix aoc.gen or mix aoc.

aoc/3 and use AOC

Internally, aoc/3 generates a module with a predefined name (Y<year>.D<day>) which contains a use AOC, day: <day>, year: <year> statement. In turn, the __using__/1 macro defined in this module is responsible for generating helper functions. Thus, if you prefer to use a different naming scheme than the one imposed by aoc/3, you can write your module as follows:

defmodule MySolution do
  use AOC, day: <day>, year: <year>

  ...
end

When the AOC module is used like this, the helper functions defined below are still usable, but the helpers in AOC.IEx will not work.

Helper functions

This module defines various functions such as input_path/2, input_string/2, input_stream/2 and their example_*/2 counter parts. Inside the generated module, helpers are inserted which call these functions with the module's day / year. Thus, if you call input_path() inside your solution module, it will call input_path/2 for you with the module's day and year, obtaining the path to the appropriate input file.

The following table provides an overview of the inserted functions and their counterparts in this module:

GeneratedCalls
input_path/0input_path/2
input_string/0input_string/2
input_stream/0input_stream/2
example_path/0example_path/2
example_string/0example_string/2
example_stream/0example_stream/2

The generated functions are overridable, i.e. you can define your own version of these functions which will overwrite the generated function. This is handy to do something like the following:

def input_stream, do: super() |> Stream.map(&String.to_integer/1)

Link to this section Summary

Functions

Generate an advent of code solution module for a given year and day.

Get the example path for year, day.

Stream the contents of the example for year, day.

Get the example contents of year, day.

Get the input path for year, day.

Stream the contents of the input for year, day.

Get the input contents of year, day.

Link to this section Functions

Link to this macro

aoc(year, day, list)

View Source (macro)

Generate an advent of code solution module for a given year and day.

The generated module will be named Y<year>.D<day>. use AOC will be injected in the body of the module, so that the input helpers described in the module documentation are available.

Examples

import AOC

aoc 2020, 1 do
  def some_function do
    :foo
  end

end

is equivalent to:

defmodule Y2020.D1 do
  use AOC

  def some_function do
    :foo
  end
end

Get the example path for year, day.

Obtains the path where mix aoc.get stores the input for year, day. This path defaults to input/<year>_<day>_example.txt, but can be customized. Please refer to the mix aoc.get documentation for more information.

Link to this function

example_stream(year, day)

View Source

Stream the contents of the example for year, day.

The stream is created by calling File.stream!/1 on the path returned by example_path/2. Afterwards, String.trim/1 is mapped over the stream (using Stream.map/2), to remove trailing newlines and whitespace.

Link to this function

example_string(year, day)

View Source

Get the example contents of year, day.

Obtained by calling File.read!/1 on the path returned by example_path/2.

Get the input path for year, day.

Obtains the path where mix aoc.get stores the input for year, day. This path defaults to input/<year>_<day>.txt, but can be customized. Please refer to the mix aoc.get documentation for more information.

Stream the contents of the input for year, day.

The stream is created by calling File.stream!/1 on the path returned by input_path/2. Afterwards, String.trim/1 is mapped over the stream (using Stream.map/2), to remove trailing newlines and whitespace.

Get the input contents of year, day.

Obtained by calling File.read!/1 on the path returned by input_path/2.