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

Advent of Code - 2024 Day 1

2024/day-01.livemd

Advent of Code - 2024 Day 1

Mix.install([
  {:req, "~> 0.5.8"},
  {:kino_aoc, "~> 0.1.7"}
])

Part 1

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2024", "1", System.fetch_env!("LB_AOC_SESSION"))
input =
  puzzle_input
  |> String.split()
  |> Enum.map(&String.to_integer/1)
  |> Enum.chunk_every(2)
input
|> Enum.zip_with(&Function.identity/1)
|> Enum.map(&Enum.sort/1)
|> Enum.zip_with(fn [a, b] ->
  abs(a - b)
end)
|> Enum.sum()

Part 2

[left, right] = Enum.zip_with(input, &Function.identity/1)

left = Enum.uniq(left)

freqs = Enum.frequencies(right)

for n <- left, reduce: 0 do
  sum -> sum + n * (freqs[n] || 0)
end