diff --git a/README.md b/README.md index 312f868..f12a5d7 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ My Elixir solutions for [Advent of Code](https://adventofcode.com/) (all years). -
- +
diff --git a/lib/y2024/README.md b/lib/y2024/README.md
index bc2689c..ecc43d1 100644
--- a/lib/y2024/README.md
+++ b/lib/y2024/README.md
@@ -2,7 +2,7 @@
My Elixir solutions for [Advent of Code 2024](https://adventofcode.com/2024).
-
+
## Benchmarks
@@ -47,4 +47,5 @@ day 16, part 1 12.91 77.43 ms ±5.49% 77.14 ms 86.
day 16, part 2 13.07 76.51 ms ±5.00% 76.09 ms 85.69 ms
day 17, part 1 45.64 K 21.91 μs ±15.81% 21.17 μs 37.50 μs
day 18, part 1 0.27 3.73 s ±0.03% 3.73 s 3.73 s
+day 18, part 2 0.0250 39.94 s ±0.00% 39.94 s 39.94 s
```
diff --git a/lib/y2024/day18.ex b/lib/y2024/day18.ex
index dcad4cc..dbee1db 100644
--- a/lib/y2024/day18.ex
+++ b/lib/y2024/day18.ex
@@ -3,7 +3,7 @@ defmodule Y2024.Day18 do
alias Advent.PathGrid
- def part1(bytes, size \\ 50, byte_num \\ 1024) do
+ def part1(bytes, size \\ 70, byte_num \\ 1024) do
fallen = Enum.take(bytes, byte_num)
graph =
@@ -14,13 +14,17 @@ defmodule Y2024.Day18 do
length(Graph.get_shortest_path(graph, {0, 0}, {size, size})) - 1
end
- # @doc """
- # iex> Day18.part2("update or delete me")
- # "update or delete me"
- # """
- # def part2(input) do
- # input
- # end
+ def part2(bytes, size \\ 70) do
+ Enum.reduce_while(bytes, empty_grid({0, 0}, {size, size}), fn {row, col}, graph ->
+ graph = PathGrid.add_wall(graph, {row, col})
+
+ if Graph.get_shortest_path(graph, {0, 0}, {size, size}) == nil do
+ {:halt, "#{row},#{col}"}
+ else
+ {:cont, graph}
+ end
+ end)
+ end
@doc """
iex> Day18.parse_input("5,4\\n4,2\\n4,5\\n3,0\\n")
@@ -58,5 +62,5 @@ defmodule Y2024.Day18 do
end
def part1_verify, do: input() |> parse_input() |> part1(70, 1024)
- # def part2_verify, do: input() |> parse_input() |> part2()
+ def part2_verify, do: input() |> parse_input() |> part2()
end
diff --git a/test/y2024/day18_test.exs b/test/y2024/day18_test.exs
index 491e930..531349f 100644
--- a/test/y2024/day18_test.exs
+++ b/test/y2024/day18_test.exs
@@ -7,8 +7,14 @@ defmodule Y2024.Day18Test do
assert test_data() |> Day18.parse_input() |> Day18.part1(6, 12) == 22
end
+ test "part2" do
+ assert test_data() |> Day18.parse_input() |> Day18.part2(6) == "6,1"
+ end
+
test "verification, part 1", do: assert(Day18.part1_verify() == 436)
- # test "verification, part 2", do: assert(Day18.part2_verify() == "update or delete me")
+
+ @tag timeout: :infinity
+ test "verification, part 2", do: assert(Day18.part2_verify() == "61,50")
def test_data(), do: File.read!("test/y2024/input/day18/sample.txt")
end