1
0
Fork 0

feat: pad numbers

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:53 -04:00
parent a691461882
commit b3a07a5c0f
7 changed files with 25 additions and 34 deletions

View file

@ -5,8 +5,17 @@ defmodule AdventOfCode do
def solver(selector) do def solver(selector) do
[day, part] = String.split(selector, ".") [day, part] = String.split(selector, ".")
day_module = Macro.camelize("Day#{day}") part_module(day, part)
part_module = Macro.camelize("Part#{part}")
Module.concat([AdventOfCode, day_module, part_module])
end end
def part_module(day, part) do
Module.concat(day_module(day), Macro.camelize("Part#{part_number(part)}"))
end
def day_module(day) do
Module.concat(AdventOfCode, Macro.camelize("Day#{day_number(day)}"))
end
defp day_number(day), do: String.pad_leading(day, 2, "0")
defp part_number(part), do: String.trim_leading(part, "0")
end end

View file

@ -1,11 +0,0 @@
defmodule AdventOfCode.Day0.Part0 do
alias AdventOfCode.PuzzleSolver
use PuzzleSolver
@impl PuzzleSolver
def solve(stream) do
Stream.run(stream)
"42"
end
end

View file

@ -3,11 +3,6 @@ defmodule Mix.AdventOfCode do
Helpers for `AdventOfCode` mix tasks. Helpers for `AdventOfCode` mix tasks.
""" """
def day_module(day) do defdelegate day_module(day), to: AdventOfCode
Module.concat(AdventOfCode, Macro.camelize("Day#{day}")) defdelegate part_module(day, part), to: AdventOfCode
end
def part_module(day, part) do
Module.concat(day_module(day), Macro.camelize("Part#{part}"))
end
end end

View file

@ -1,3 +0,0 @@
foo
bar
baz

View file

@ -1,7 +0,0 @@
defmodule AdventOfCode.Day0.Part0Test do
use AdventOfCode.PuzzleCase, module: AdventOfCode.Day0.Part0
test "returns the answer to live the universe and everything" do
assert_solution "life the universe and everything", "42"
end
end

View file

@ -5,9 +5,17 @@ defmodule AdventOfCodeTest do
describe "solver/1" do describe "solver/1" do
test "returns a module for a solver" do test "returns a module for a solver" do
assert solver("1.1") == AdventOfCode.Day1.Part1 assert solver("1.1") == AdventOfCode.Day01.Part1
assert solver("2.2") == AdventOfCode.Day2.Part2 assert solver("2.2") == AdventOfCode.Day02.Part2
assert solver("3.1") == AdventOfCode.Day3.Part1 assert solver("3.1") == AdventOfCode.Day03.Part1
end
test "returns a solver module for zero padded numbers" do
assert solver("01.01") == AdventOfCode.Day01.Part1
assert solver("02.2") == AdventOfCode.Day02.Part2
assert solver("3.01") == AdventOfCode.Day03.Part1
assert solver("12.1") == AdventOfCode.Day12.Part1
assert solver("24.02") == AdventOfCode.Day24.Part2
end end
end end
end end