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

Advent 2023 - Day 6

day6.livemd

Advent 2023 - Day 6

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

Section

input = Kino.Input.textarea("Please paste your input file")
input =
  input
  |> Kino.Input.read()
  |> String.split("\n")
  |> Enum.map(fn line ->
    line
    |> String.split(" ")
    |> Enum.map(&Integer.parse/1)
    |> Enum.filter(&(&1 != :error))
    |> Enum.map(&elem(&1, 0))
  end)
[[7, 15, 30], [9, 40, 200]]

Part 1

input
|> Enum.zip()
|> Enum.map(fn {ms, record} ->
  range = 1..(ms - 1)

  for held <- range do
    time_left = ms - held
    held * time_left
  end
  |> Enum.filter(&amp;(&amp;1 > record))
  |> Enum.count()
end)
|> Enum.reduce(fn x, acc -> x * acc end)
288

Part 2

[ms, record] =
  input
  |> Enum.map(fn num ->
    num
    |> Enum.join()
    |> Integer.parse()
    |> elem(0)
  end)

range = 1..(ms - 1)

Enum.reduce(range, 0, fn held, records ->
  time_left = ms - held
  distance = held * time_left

  if distance > record do
    records + 1
  else
    records
  end
end)
71503