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

Advent of Code 2024 - Day 01

2024/day-01.livemd

Advent of Code 2024 - Day 01

Mix.install([{:kino, github: "livebook-dev/kino"}])

kino_input = Kino.Input.textarea("Please paste your input file: ")

Part 1

input = Kino.Input.read(kino_input)

input
|> String.split("\n", trim: true)
|> Enum.map(&String.split/1)
|> Enum.map(fn [a, b] -> {String.to_integer(a), String.to_integer(b)} end)
|> Enum.unzip()
|> Tuple.to_list()
|> Enum.map(&Enum.sort/1)
|> Enum.zip()
|> Enum.map(fn {a, b} -> abs(a - b) end)
|> Enum.sum()

Part 2

input = Kino.Input.read(kino_input)

input
|> String.split("\n", trim: true)
|> Enum.map(&String.split/1)
|> Enum.map(fn [a, b] -> {String.to_integer(a), String.to_integer(b)} end)
|> Enum.unzip()
|> (fn {left_list, right_list} ->
  frequencies = Enum.frequencies(right_list)

  Enum.reduce(left_list, 0, fn num, acc ->
    acc + (num * Map.get(frequencies, num, 0))
  end)
end).()