aoc/2022/lib/day.ex

47 lines
1 KiB
Elixir
Raw Normal View History

2023-04-17 18:18:20 +00:00
defmodule AOC.Day do
defmacro __using__(opts) do
day = Keyword.get(opts, :day)
debug = Keyword.get(opts, :debug, false)
trim = Keyword.get(opts, :trim, true)
input_file = Keyword.get(opts, :input, nil)
quote do
def solve do
input =
AOC.Day.input_lines(unquote(day), unquote(trim), unquote(input_file))
|> parse_input()
if unquote(debug) do
IO.inspect(input)
end
part1_solution = part1(input)
IO.puts("Part 1: #{part1_solution}")
part2_solution = part2(input)
IO.puts("Part 2: #{part2_solution}")
end
end
end
def input_lines(day, trim, input_file) do
lines =
input_file_name(day, input_file)
|> File.stream!()
if trim do
Enum.map(lines, &String.trim/1)
else
lines
end
end
def input_file_name(day, input_file) do
if input_file do
Path.join([File.cwd!(), "inputs", "day#{day}_#{input_file}.txt"])
else
Path.join([File.cwd!(), "inputs", "day#{day}.txt"])
end
end
end