Skip to content

Commit

Permalink
2024 day 18, part 2
Browse files Browse the repository at this point in the history
Okay now that I know the answer, I can write an implementation that doesn't suck
  • Loading branch information
sevenseacat committed Dec 18, 2024
1 parent 7ef665a commit 8a3b7b7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
My Elixir solutions for [Advent of Code](https://adventofcode.com/) (all years).

<!-- stars start -->
<p><img src="https://img.shields.io/static/v1?label=Total&message=452%20stars&style=for-the-badge&color=green" alt="452 stars" /></p>
<p><a href="./lib/y2024/"><img src="https://img.shields.io/static/v1?label=2024&message=33%20stars&style=for-the-badge&color=yellow" alt="33 stars" /></a><br />
<p><img src="https://img.shields.io/static/v1?label=Total&message=453%20stars&style=for-the-badge&color=green" alt="453 stars" /></p>
<p><a href="./lib/y2024/"><img src="https://img.shields.io/static/v1?label=2024&message=34%20stars&style=for-the-badge&color=yellow" alt="34 stars" /></a><br />
<a href="./lib/y2023/"><img src="https://img.shields.io/static/v1?label=2023&message=44%20stars&style=for-the-badge&color=green" alt="44 stars" /></a><br />
<a href="./lib/y2022/"><img src="https://img.shields.io/static/v1?label=2022&message=%E2%AD%90%EF%B8%8F%2050%20stars%20%E2%AD%90%EF%B8%8F&style=for-the-badge&color=brightgreen" alt="50 stars" /></a><br />
<a href="./lib/y2021/"><img src="https://img.shields.io/static/v1?label=2021&message=46%20stars&style=for-the-badge&color=green" alt="46 stars" /></a><br />
Expand Down
3 changes: 2 additions & 1 deletion lib/y2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

My Elixir solutions for [Advent of Code 2024](https://adventofcode.com/2024).

<!-- stars 2024 start --><img src="https://img.shields.io/static/v1?label=2024&message=33%20stars&style=for-the-badge&color=yellow" alt="33 stars" /><!-- stars 2024 end -->
<!-- stars 2024 start --><img src="https://img.shields.io/static/v1?label=2024&message=34%20stars&style=for-the-badge&color=yellow" alt="34 stars" /><!-- stars 2024 end -->

## Benchmarks

Expand Down Expand Up @@ -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
```
22 changes: 13 additions & 9 deletions lib/y2024/day18.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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")
Expand Down Expand Up @@ -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
8 changes: 7 additions & 1 deletion test/y2024/day18_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8a3b7b7

Please sign in to comment.