From 8346a50620793bb4338a4cf2b63ea949c28abe6f Mon Sep 17 00:00:00 2001 From: "Ruidy (aider)" Date: Sun, 1 Dec 2024 09:53:12 +0100 Subject: [PATCH] feat: Implement calculate_total_distance function with sorting and absolute difference calculation --- lib/advent_code2024.ex | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/advent_code2024.ex b/lib/advent_code2024.ex index 50127dd..6c84a98 100644 --- a/lib/advent_code2024.ex +++ b/lib/advent_code2024.ex @@ -4,15 +4,22 @@ defmodule AdventCode2024 do """ @doc """ - Hello world. + Calculates the total distance between two lists of numbers. + Lists are first sorted, then paired up, and the absolute differences are summed. ## Examples - iex> AdventCode2024.hello() - :world + iex> AdventCode2024.calculate_total_distance([3, 4, 2, 1, 3, 3], [4, 3, 5, 3, 9, 3]) + 11 + iex> AdventCode2024.calculate_total_distance([], []) + 0 + iex> AdventCode2024.calculate_total_distance([1], [3]) + 2 """ - def hello do - :world + def calculate_total_distance(left_list, right_list) do + Enum.zip(Enum.sort(left_list), Enum.sort(right_list)) + |> Enum.map(fn {a, b} -> abs(a - b) end) + |> Enum.sum() end end