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

Advent of Code 2023 Day 6 Part 1

2023_day6_part1.livemd

Advent of Code 2023 Day 6 Part 1

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.map(&String.to_integer(&1))
    end)
    |> Enum.zip()
    |> Enum.map(fn {time, distance} ->
      %{
        time: time,
        distance: distance
      }
    end)
  end

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