Add 2024/day1/part1.erl
This commit is contained in:
parent
e9f71df35e
commit
80a0b71a15
|
@ -0,0 +1,23 @@
|
|||
-module(day1).
|
||||
-export([main/0]).
|
||||
|
||||
|
||||
main() ->
|
||||
{ok, Content} = file:read_file("input.txt"),
|
||||
Lines = string:split(string:trim(Content), "\n", all),
|
||||
{List1, List2} = lists:foldl(fun parse_line/2, {[], []}, Lines),
|
||||
SortedList1 = lists:sort(List1),
|
||||
SortedList2 = lists:sort(List2),
|
||||
Distance = calculate_distance(SortedList1, SortedList2),
|
||||
io:format("Result: ~p~n", [Distance]).
|
||||
|
||||
|
||||
parse_line(Line, {Acc1, Acc2}) ->
|
||||
[Str1, Str2] = string:split(string:trim(Line), " ", all),
|
||||
{Num1, _} = string:to_integer(Str1),
|
||||
{Num2, _} = string:to_integer(Str2),
|
||||
{[Num1 | Acc1], [Num2 | Acc2]}.
|
||||
|
||||
|
||||
calculate_distance(List1, List2) ->
|
||||
lists:sum([abs(A - B) || {A, B} <- lists:zip(List1, List2)]).
|
Loading…
Reference in New Issue