-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fffa13b
commit 04cb253
Showing
4 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time: 40 70 98 79 | ||
Distance: 215 1051 2147 1005 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |