mirror of
https://github.com/rjNemo/ai_advent_code_2024
synced 2026-06-06 02:26:44 +00:00
feat: Implement calculate_total_distance function with sorting and absolute difference calculation
This commit is contained in:
parent
881c7335a0
commit
8346a50620
1 changed files with 12 additions and 5 deletions
|
|
@ -4,15 +4,22 @@ defmodule AdventCode2024 do
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@doc """
|
@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
|
## Examples
|
||||||
|
|
||||||
iex> AdventCode2024.hello()
|
iex> AdventCode2024.calculate_total_distance([3, 4, 2, 1, 3, 3], [4, 3, 5, 3, 9, 3])
|
||||||
:world
|
11
|
||||||
|
iex> AdventCode2024.calculate_total_distance([], [])
|
||||||
|
0
|
||||||
|
iex> AdventCode2024.calculate_total_distance([1], [3])
|
||||||
|
2
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def hello do
|
def calculate_total_distance(left_list, right_list) do
|
||||||
:world
|
Enum.zip(Enum.sort(left_list), Enum.sort(right_list))
|
||||||
|
|> Enum.map(fn {a, b} -> abs(a - b) end)
|
||||||
|
|> Enum.sum()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue