From bf6fa2af47a049b5c35ee1dc8d8b2a7c6e1b6daf Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Sun, 1 Dec 2024 14:27:54 +0900 Subject: [PATCH] Replace custom function with Enum.zip_with --- 2024/day1.exs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/2024/day1.exs b/2024/day1.exs index 7602f74..adace77 100755 --- a/2024/day1.exs +++ b/2024/day1.exs @@ -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)