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

--- Day 6: Wait For It ---

2023/day_6.livemd

— Day 6: Wait For It —

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

Section

test_input = Kino.Input.textarea("test_input")
score_sheet = Kino.Input.textarea("score_sheet")
defmodule WaitForIt do
  def part_one(input) do
    input
    |> parse()
    |> Enum.map(&winning_hold_times/1)
    |> Enum.map(&length/1)
    |> Enum.product()
  end

  def winning_hold_times(time, dist) do
    0..time
    |> Enum.map(&(&1 * (time - &1)))
    |> Enum.filter(&(&1 > dist))
  end

  def winning_hold_times({t, d}), do: winning_hold_times(t, d)
  def winning_hold_times([t, d]), do: winning_hold_times(t, d)

  def parse(input) do
    input
    |> Kino.Input.read()
    |> String.split("\n")
    |> Enum.map(fn
      "Time:" <> times -> split(times)
      "Distance:" <> distances -> split(distances)
    end)
    |> Enum.zip()
  end

  def split(list) do
    list
    |> String.split()
    |> Enum.map(&amp;String.to_integer/1)
  end

  def join(list) do
    list
    |> String.split()
    |> Enum.join()
    |> String.to_integer()
  end

  def part_two(input) do
    input
    |> Kino.Input.read()
    |> String.split("\n")
    |> Enum.map(fn
      "Time:" <> times -> join(times)
      "Distance:" <> distances -> join(distances)
    end)
    |> winning_hold_times()
    |> length()
  end
end
WaitForIt.part_one(score_sheet)
WaitForIt.part_two(score_sheet)