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(&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(&(distance < &1))
|> Enum.count()
end
def run(input) do
parse(input)
|> Enum.map(&ways/1)
end
end
Part1.run(example) == expected
true
Kino.Input.read(input)
|> Part1.run()
|> Enum.product()
|> then(&Kino.Markdown.new("Part 1: #{&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(&Kino.Markdown.new("Part 2: #{&1}"))