Replace custom function with Enum.zip_with
This commit is contained in:
parent
b26f687dab
commit
bf6fa2af47
1 changed files with 1 additions and 4 deletions
|
@ -4,12 +4,9 @@ defmodule Day1 do
|
||||||
list1 = Enum.sort(list1)
|
list1 = Enum.sort(list1)
|
||||||
list2 = Enum.sort(list2)
|
list2 = Enum.sort(list2)
|
||||||
|
|
||||||
distances(list1, list2) |> Enum.sum()
|
Enum.zip_with(list1, list2, &abs(&1 - &2)) |> Enum.sum()
|
||||||
end
|
end
|
||||||
|
|
||||||
def distances([], []), do: []
|
|
||||||
def distances([h1 | t1], [h2 | t2]), do: [abs(h1 - h2) | distances(t1, t2)]
|
|
||||||
|
|
||||||
def part2({list1, list2}) do
|
def part2({list1, list2}) do
|
||||||
frequencies = Enum.frequencies(list2)
|
frequencies = Enum.frequencies(list2)
|
||||||
Enum.reduce(list1, 0, fn item, count -> count + item * Map.get(frequencies, item, 0) end)
|
Enum.reduce(list1, 0, fn item, count -> count + item * Map.get(frequencies, item, 0) end)
|
||||||
|
|
Loading…
Reference in a new issue