You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
1.1 KiB
31 lines
1.1 KiB
-module(day25).
|
|
|
|
-export([solve/1]).
|
|
|
|
solve(Input) ->
|
|
RoundData = binary:split(Input, <<$\n>>, [trim_all, global]),
|
|
{part1(RoundData), none}.
|
|
|
|
part1(Input) -> int_to_snafu(lists:sum(lists:map(fun snafu_to_int/1, Input))).
|
|
|
|
snafu_to_int(Snafu) -> snafu_to_int(Snafu, 0).
|
|
|
|
snafu_to_int(<<>>, Acc) -> Acc;
|
|
snafu_to_int(<<$2, Rest/binary>>, Acc) -> snafu_to_int(Rest, Acc * 5 + 2);
|
|
snafu_to_int(<<$1, Rest/binary>>, Acc) -> snafu_to_int(Rest, Acc * 5 + 1);
|
|
snafu_to_int(<<$0, Rest/binary>>, Acc) -> snafu_to_int(Rest, Acc * 5);
|
|
snafu_to_int(<<$-, Rest/binary>>, Acc) -> snafu_to_int(Rest, Acc * 5 - 1);
|
|
snafu_to_int(<<$=, Rest/binary>>, Acc) -> snafu_to_int(Rest, Acc * 5 - 2).
|
|
|
|
int_to_snafu(0) -> <<"0">>;
|
|
int_to_snafu(N) -> int_to_snafu(N, []).
|
|
|
|
int_to_snafu(0, Acc) -> list_to_binary(Acc);
|
|
int_to_snafu(Num, Acc) ->
|
|
case Num rem 5 of
|
|
0 -> int_to_snafu(Num div 5, [$0 | Acc]);
|
|
1 -> int_to_snafu(Num div 5, [$1 | Acc]);
|
|
2 -> int_to_snafu(Num div 5, [$2 | Acc]);
|
|
3 -> int_to_snafu(Num div 5 + 1, [$= | Acc]);
|
|
4 -> int_to_snafu(Num div 5 + 1, [$- | Acc])
|
|
end.
|