Replace custom function with Enum.zip_with

This commit is contained in:
Adam Millerchip 2024-12-01 14:27:54 +09:00
parent b26f687dab
commit bf6fa2af47

View file

@ -4,12 +4,9 @@ defmodule Day1 do
list1 = Enum.sort(list1)
list2 = Enum.sort(list2)
distances(list1, list2) |> Enum.sum()
Enum.zip_with(list1, list2, &abs(&1 - &2)) |> Enum.sum()
end
def distances([], []), do: []
def distances([h1 | t1], [h2 | t2]), do: [abs(h1 - h2) | distances(t1, t2)]
def part2({list1, list2}) do
frequencies = Enum.frequencies(list2)
Enum.reduce(list1, 0, fn item, count -> count + item * Map.get(frequencies, item, 0) end)