AdventOfCode/dayN.exs

52 lines
1.1 KiB
Elixir
Raw Normal View History

2023-12-02 06:00:28 +00:00
#!/usr/bin/env elixir
2022-11-27 09:04:58 +00:00
defmodule DayREPLACE_ME do
def part1(input) do
input
end
def part2(_input) do
:ok
2022-11-27 09:04:58 +00:00
end
def input do
2022-11-27 09:04:58 +00:00
with [input_filename] <- System.argv(),
{:ok, input} <- File.read(input_filename) do
input
2022-11-27 09:04:58 +00:00
else
2022-11-27 16:20:58 +00:00
_ -> :error
end
end
#######################
# HERE BE BOILERPLATE #
#######################
def run do
case input() do
:error -> print_usage()
input -> run_parts_with_timer(input)
2022-11-27 09:04:58 +00:00
end
end
2022-11-27 17:16:26 +00:00
defp run_parts_with_timer(input) do
2022-11-28 01:18:27 +00:00
run_with_timer(1, fn -> part1(input) end)
run_with_timer(2, fn -> part2(input) end)
end
defp run_with_timer(part, fun) do
{time, result} = :timer.tc(fun)
IO.puts("Part #{part} (completed in #{format_time(time)}):\n")
2023-12-02 06:00:28 +00:00
IO.puts("#{inspect result}\n")
2022-11-27 09:04:58 +00:00
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), do: "#{μsec / 1_000_000}s"
2022-11-27 09:04:58 +00:00
defp print_usage do
IO.puts("Usage: elixir dayREPLACE_ME.exs input_filename")
end
end
2022-11-27 16:20:58 +00:00
# DayREPLACE_ME.run()