Current section
Files
Jump to
Current section
Files
src/gladvent@internal@cmd.erl
-module(gladvent@internal@cmd).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([input_dir/1, src_dir/1, exec/4, year_flag/0]).
-export_type([timing/0]).
-type timing() :: endless | {ending, integer()}.
-spec input_dir(integer()) -> binary().
input_dir(Year) ->
<<<<"input/"/utf8, (gleam@int:to_string(Year))/binary>>/binary, "/"/utf8>>.
-spec src_dir(integer()) -> binary().
src_dir(Year) ->
<<<<<<"src/"/utf8, "aoc_"/utf8>>/binary,
(gleam@int:to_string(Year))/binary>>/binary,
"/"/utf8>>.
-spec now_ms() -> integer().
now_ms() ->
os:system_time(millisecond).
-spec task_map(list(IYP), fun((IYP) -> IYR)) -> list({IYP,
gleam@otp@task:task(IYR)}).
task_map(L, F) ->
gleam@list:map(
L,
fun(X) -> {X, gleam@otp@task:async(fun() -> F(X) end)} end
).
-spec try_await_many(list({IYU, gleam@otp@task:task(IYV)}), timing()) -> list({IYU,
{ok, IYV} | {error, binary()}}).
try_await_many(Tasks, Timing) ->
case Timing of
endless ->
gleam@list:map(
Tasks,
fun(Tup) ->
gleam@pair:map_second(
Tup,
fun(T) -> {ok, gleam@otp@task:await_forever(T)} end
)
end
);
{ending, Timeout} ->
End = now_ms() + Timeout,
gleam@list:map(
Tasks,
fun(Tup@1) ->
gleam@pair:map_second(
Tup@1,
fun(T@1) ->
Res = begin
_pipe = End - now_ms(),
_pipe@1 = gleam@int:clamp(_pipe, 0, Timeout),
gleam@otp@task:try_await(T@1, _pipe@1)
end,
gleam@result:map_error(Res, fun(Err) -> case Err of
timeout ->
<<"task timed out"/utf8>>;
{exit, S} ->
<<"task exited for some reason: "/utf8,
(gleam@string:inspect(S))/binary>>
end end)
end
)
end
)
end.
-spec exec(
list(integer()),
timing(),
fun((integer()) -> IYJ),
fun(({integer(), {ok, IYJ} | {error, binary()}}) -> IYM)
) -> list(IYM).
exec(Days, Timing, Do, Collect) ->
_pipe = Days,
_pipe@1 = task_map(_pipe, Do),
_pipe@2 = try_await_many(_pipe@1, Timing),
gleam@list:map(_pipe@2, Collect).
-spec current_year() -> integer().
current_year() ->
erlang:element(1, (erlang:element(1, erlang:localtime()))).
-spec year_flag() -> glint@flag:flag_builder(integer()).
year_flag() ->
_pipe = glint@flag:int(),
_pipe@1 = glint@flag:default(_pipe, current_year()),
glint@flag:constraint(_pipe@1, fun(Year) -> case Year < 2015 of
true ->
snag:error(
<<"advent of code did not exist prior to 2015, did you mistype?"/utf8>>
);
false ->
{ok, nil}
end end).