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

Day 6

advent_of_code/2023/day-06.livemd

Day 6

Day 6: Wait For It

Day 6: Wait For It

Input

input = """
Time:      7  15   30
Distance:  9  40  200
"""

Part 1

defmodule Awesome do
  def run(input) do
    races(input)
    |> Enum.map(fn {time, distance} -> solve(time, distance) end)
    |> Enum.product()
  end

  defp solve(time, distance) do
    1..time
    |> Enum.filter(fn t ->
      speed = t
      left_time = time - t
      left_time * speed > distance
    end)
    |> Enum.count()
  end

  defp races(input) do
    input
    |> String.split("\n", trim: true)
    |> Enum.map(fn s ->
      s
      |> String.split(":")
      |> Enum.at(-1)
      |> String.split(" ", trim: true)
      |> Enum.map(&String.to_integer/1)
    end)
    |> Enum.zip()
  end
end
Awesome.run(input)
puzzle_input = """
Time:        57     72     69     92
Distance:   291   1172   1176   2026
"""
Awesome.run(puzzle_input)

Part 2

defmodule Awesome2 do
  def run(input) do
    [time, distance] = races(input)

    solve(time, distance)
  end

  defp solve(time, distance) do
    1..time
    |> Enum.filter(fn t ->
      speed = t
      left_time = time - t
      left_time * speed > distance
    end)
    |> Enum.count()
  end

  defp races(input) do
    input
    |> String.split("\n", trim: true)
    |> Enum.map(fn s ->
      s
      |> String.split(":")
      |> Enum.at(-1)
      |> String.split(" ", trim: true)
      |> Enum.join()
      |> String.to_integer()
    end)
  end
end
Awesome2.run(input)
Awesome2.run(puzzle_input)