Solutions to Advent of Code 2023 - solved in Elixir in order to learn the language.
Repo template taken from https://github.com/mhanberg/advent-of-code-elixir-starter
There are 25 modules, 25 tests, and 50 mix tasks.
- Fill in the tests with the example solutions.
- Write your implementation.
- Fill in the final problem inputs into the mix task and run
mix d01.p1
!- Benchmark your solution by passing the
-b
flag,mix d01.p1 -b
- Benchmark your solution by passing the
defmodule AdventOfCode.Day01 do
def part1(args) do
end
def part2(args) do
end
end
defmodule AdventOfCode.Day01Test do
use ExUnit.Case
import AdventOfCode.Day01
@tag :skip # Make sure to remove to run your test.
test "part1" do
input = nil
result = part1(input)
assert result
end
@tag :skip # Make sure to remove to run your test.
test "part2" do
input = nil
result = part2(input)
assert result
end
end
defmodule Mix.Tasks.D01.P1 do
use Mix.Task
import AdventOfCode.Day01
@shortdoc "Day 01 Part 1"
def run(args) do
input = AdventOfCode.Input.get!(1, 2020)
if Enum.member?(args, "-b"),
do: Benchee.run(%{part_1: fn -> input |> part1() end}),
else:
input
|> part1()
|> IO.inspect(label: "Part 1 Results")
end
end
This starter comes with a module that will automatically get your inputs so you
don't have to mess with copy/pasting. Don't worry, it automatically caches your
inputs to your machine so you don't have to worry about slamming the Advent of
Code server. You will need to configure it with your cookie and make sure to
enable it. You can do this by creating a config/secrets.exs
file containing
the following:
import Config
config :advent_of_code, AdventOfCode.Input,
allow_network?: true,
session_cookie: "..." # yours will be longer
After which, you can retrieve your inputs using the module:
day = 1
year = 2020
AdventOfCode.Input.get!(day, year)
# or just have it auto-detect the current year
AdventOfCode.Input.get!(7)
# and if your input somehow gets mangled and you need a fresh one:
AdventOfCode.Input.delete!(7, 2019)
# and the next time you `get!` it will download a fresh one -- use this sparingly!