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

Day 6

2023/06.livemd

Day 6

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

Input

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

input = Kino.Input.textarea("Input", default: example)

Part 1

expected = [4, 8, 9]
[4, 8, 9]
defmodule Part1 do
  def parse(input) do
    for line <- String.split(input, "\n", trim: true) do
      Regex.scan(~r/\d+/, line)
      |> List.flatten()
      |> Enum.map(&amp;String.to_integer/1)
    end
    |> Enum.zip()
  end

  def ways({time, distance}) do
    Enum.map(0..time, fn hold ->
      remaining = time - hold
      hold * remaining
    end)
    |> Enum.filter(&amp;(distance < &amp;1))
    |> Enum.count()
  end

  def run(input) do
    parse(input)
    |> Enum.map(&amp;ways/1)
  end
end

Part1.run(example) == expected
true
Kino.Input.read(input)
|> Part1.run()
|> Enum.product()
|> then(&amp;Kino.Markdown.new("Part 1: #{&amp;1}"))

Part 2

expected = 71503
71503
defmodule Part2 do
  def parse(input) do
    for line <- String.split(input, "\n", trim: true) do
      Regex.scan(~r/\d+/, line)
      |> Enum.join()
      |> String.to_integer()
    end
  end

  def run(input) do
    [time, distance] = parse(input)
    Part1.ways({time, distance})
  end
end

Part2.run(example) == expected
true
Kino.Input.read(input)
|> Part2.run()
|> then(&amp;Kino.Markdown.new("Part 2: #{&amp;1}"))