Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 1

2024/day01.livemd

Day 1

Mix.install([{:utils, path: "#{__DIR__}/utils"}])

Solution

lists =
  "day01"
  |> Utils.read_2d_ints()
  |> Enum.zip_with(&(&1))
lists
|> Enum.map(&Enum.sort/1)
|> Enum.zip_with(fn [a, b] -> abs(a - b) end)
|> Enum.sum()
[left, right] = lists
freq = Enum.frequencies(right)

left
|> Enum.map(fn id -> id * Map.get(freq, id, 0) end)
|> Enum.sum()