Add new Elixir template

This commit is contained in:
Adam Millerchip 2022-11-27 18:04:58 +09:00
parent 4955687b53
commit cb850a5b5e
2 changed files with 48 additions and 9 deletions

48
template/dayN.exs Normal file
View File

@ -0,0 +1,48 @@
defmodule DayREPLACE_ME do
def part1(input) do
input
end
def part2(input) do
input
end
def run do
with [input_filename] <- System.argv(),
{:ok, input} <- File.read(input_filename) do
run_parts_with_timer(input)
else
_ -> print_usage()
end
end
def run_parts_with_timer(input) do
{p1_microsecs, p1_result} = :timer.tc(fn -> part1(input) end)
IO.puts("Part 1 (completed in #{format_time(p1_microsecs)}):")
IO.write("\n")
IO.puts(p1_result)
IO.write("\n")
{p2_microsecs, p2_result} = :timer.tc(fn -> part2(input) end)
IO.puts("Part 2 (completed in #{format_time(p2_microsecs)}):")
IO.write("\n")
IO.puts(p2_result)
end
defp format_time(μsec) when μsec < 1_000, do: "#{μsec}μs"
defp format_time(μsec) when μsec < 1_000_000, do: "#{μsec / 1000}ms"
defp format_time(μsec) when μsec < 60_000_000, do: "#{μsec / 1_000_000}s"
defp format_time(μsec) when μsec < 3_660_000_000 do
total_secs = round(μsec / 1_000_000)
mins = div(total_secs, 60)
secs = rem(total_secs, 60)
"#{mins}m#{secs}s"
end
defp print_usage do
IO.puts("Usage: elixir dayREPLACE_ME.exs input_filename")
end
end
DayREPLACE_ME.run()

View File

@ -1,9 +0,0 @@
defmodule DayREPLACE_MEPart1 do
def run do
File.read!("input")
|> String.split("\n", trim: true)
|> IO.inspect()
end
end
DayREPLACE_MEPart1.run()