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

Advent of Code 2023 Day 6 Part 2

2023_day6_part2.livemd

Advent of Code 2023 Day 6 Part 2

Mix.install([
  {:kino_aoc, "~> 0.1.5"}
])

Get Inputs

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2023", "6", System.fetch_env!("LB_SESSION"))

My answer

defmodule Resolver do
  def parse(input) do
    input
    |> String.split("\n")
    |> Enum.map(fn line ->
      line
      |> String.split(" ", trim: true)
      |> tl()
      |> Enum.join()
      |> String.to_integer()
    end)
    |> then(fn [time, distance] ->
      %{
        time: time,
        distance: distance
      }
    end)
  end

  def resolve(%{time: time, distance: distance}) do
    1..(time - 1)
    |> Enum.count(fn boost ->
      (time - boost) * boost > distance
    end)
  end
end
race =
  """
  Time:      7  15   30
  Distance:  9  40  200
  """
  |> String.slice(0..-2)
  |> Resolver.parse()
Resolver.resolve(race)
race = Resolver.parse(puzzle_input)
Resolver.resolve(race)