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.

15 lines
539 B

-module(day1).
-export([main/1]).
main(Args) ->
[Filename] = Args,
{ok, Filedata} = file:read_file(Filename),
Elves = binary:split(Filedata, <<"\n\n">>, [global, trim_all]),
Loads = lists:map(fun elf_load/1, Elves),
[First, Second, Third | _Rest] = lists:sort(fun (A,B) -> A >= B end, Loads),
{First, First + Second + Third}.
elf_load([]) -> 0;
elf_load([Load | Rest]) -> binary_to_integer(Load) + elf_load(Rest);
elf_load(LoadString) when is_binary(LoadString) -> elf_load(binary:split(LoadString, <<$\n>>, [global, trim_all])).