Skip to content

Commit

Permalink
[2023-06] Solution (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-flanagan authored May 24, 2024
1 parent fffa13b commit 04cb253
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ You can read more about `mix test` options
| 3 | 🌟 | | | | | 🌟 | 🌟 | 🌟 | 🌟 |
| 4 | 🌟 | | | | | 🌟 | 🌟 | 🌟 | 🌟 |
| 5 | 🌟 | | | | | 🌟 | 🌟 | 🌟 | |
| 6 | | | | | | 🌟 | 🌟 | 🌟 | |
| 6 | | | | | | 🌟 | 🌟 | 🌟 | 🌟 |
| 7 | | | | | | 🌟 | 🌟 | 🌟 | |
| 8 | | | | | | 🌟 | 🌟 | 🌟 | |
| 9 | | | | | | 🌟 | 🌟 | 🌟 | |
Expand Down
69 changes: 69 additions & 0 deletions lib/advent/y2023/d06.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
defmodule Advent.Y2023.D06 do
@moduledoc """
https://adventofcode.com/2023/day/6
"""

@doc """
"""
@spec part_one(Enumerable.t()) :: any()
def part_one(input) do
input
|> parse_input_01()
|> Enum.map(fn {t, d} ->
narrow(t, d, 1, t - 1)
end)
|> Enum.product()
end

@doc """
"""
@spec part_two(Enumerable.t()) :: any()
def part_two(input) do
[time, distance] = parse_input_02(input)
narrow(time, distance, 1, time - 1)
end

defp narrow(time, to_beat, lo, hi) do
lo_d = dist(lo, time)
hi_d = dist(hi, time)

if lo_d > to_beat && hi_d > to_beat do
Range.size(lo..hi)
else
lo = if lo_d <= to_beat, do: lo + 1, else: lo
hi = if hi_d <= to_beat, do: hi - 1, else: hi
narrow(time, to_beat, lo, hi)
end
end

defp dist(hold, time) do
hold * (time - hold)
end

@spec parse_input_01(Enumerable.t()) :: Enumerable.t()
defp parse_input_01(input) do
input
|> Enum.map(fn line ->
line
|> String.trim()
|> String.split()
|> tl()
|> Enum.map(&String.to_integer/1)
end)
|> Enum.zip()
end

@spec parse_input_02(Enumerable.t()) :: Enumerable.t()
defp parse_input_02(input) do
input
|> Enum.map(fn line ->
line
|> String.trim()
|> String.split()
|> tl()
|> Enum.map(&String.to_integer/1)
|> Enum.flat_map(&Integer.digits/1)
|> Integer.undigits()
end)
end
end
2 changes: 2 additions & 0 deletions priv/puzzle_input/y2023/d06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 40 70 98 79
Distance: 215 1051 2147 1005
16 changes: 16 additions & 0 deletions test/advent/y2023/d06_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Advent.Y2023.D06Test do
use TestHelper

@example_input [
"Time: 7 15 30",
"Distance: 9 40 200"
]

aoc_test(
example_input: @example_input,
p1_example_solution: 288,
p1_solution: 1_084_752,
p2_example_solution: 71_503,
p2_solution: 28_228_952
)
end

0 comments on commit 04cb253

Please sign in to comment.