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

AOC 2021 Day 1

elixir/livebooks/day1.livemd

AOC 2021 Day 1

Input

Mix.install([
  {:kino, "~> 0.4.1"}
])

inputwidget = Kino.Input.textarea("Enter you AOC input here")
input =
  Kino.Input.read(inputwidget)
  |> String.split()
  |> Enum.map(&String.to_integer/1)

Part 1

Count the number of times the depth measurement increases from the previous measurement

[first | remaining] = input

{result, _} =
  remaining
  |> Enum.reduce({0, first}, fn current, {count, last} ->
    if current > last do
      {count + 1, current}
    else
      {count, current}
    end
  end)

result

Part 2

Do the same thing except with a window of 3 measurements

[first | remaining] = input |> Enum.chunk_every(3, 1, :discard)

{result, _} =
  remaining
  |> Enum.reduce({0, first}, fn chunk, {count, last_chunk} ->
    if Enum.sum(chunk) > Enum.sum(last_chunk) do
      {count + 1, chunk}
    else
      {count, chunk}
    end
  end)

result