advent_of_code/elixir/livebook/2024/day1.livemd

599 B

AOC2024 Day 1

Section

input = """
3   4
4   3
2   5
1   3
3   9
3   3
"""
{left, right} = for [a,b] <- input |> String.trim()
|> String.split("\n")
|> Enum.map(&(String.split(&1, "   ") |> Enum.map(fn s -> String.to_integer(s) end))),
  reduce: {[],[]}
do
  {k,l} -> {[a|k],[b|l]}
end
for {a, b} <- (left |> Enum.sort()) |> Enum.zip((right |> Enum.sort())),
  reduce: 0 do
  acc -> acc + abs(b - a)
end
freq = right |> Enum.frequencies()
for n <- left,
  f = Map.get(freq, n, 0),
  reduce: 0 do
  acc -> acc + n*f
end