From 80a0b71a15774d2fca37b64e3cc5c5fe712b1060 Mon Sep 17 00:00:00 2001 From: x0x7 Date: Wed, 4 Dec 2024 10:30:01 -0500 Subject: [PATCH] Add 2024/day1/part1.erl --- 2024/day1/part1.erl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2024/day1/part1.erl diff --git a/2024/day1/part1.erl b/2024/day1/part1.erl new file mode 100644 index 0000000..33995e5 --- /dev/null +++ b/2024/day1/part1.erl @@ -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)]).