Skip to content

Commit

Permalink
2024 day 18, part 1
Browse files Browse the repository at this point in the history
It's super slow but I'll wait for optimization until I see what part 2 is...
  • Loading branch information
sevenseacat committed Dec 18, 2024
1 parent 2e38994 commit 7ef665a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 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=451%20stars&style=for-the-badge&color=green" alt="451 stars" /></p>
<p><a href="./lib/y2024/"><img src="https://img.shields.io/static/v1?label=2024&message=32%20stars&style=for-the-badge&color=yellow" alt="32 stars" /></a><br />
<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 />
<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=32%20stars&style=for-the-badge&color=yellow" alt="32 stars" /><!-- stars 2024 end -->
<!-- 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 -->

## Benchmarks

Expand Down Expand Up @@ -46,4 +46,5 @@ day 15, part 1 5.06 197.80 ms ±0.60% 197.59 ms 201.
day 16, part 1 12.91 77.43 ms ±5.49% 77.14 ms 86.71 ms
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
```
62 changes: 62 additions & 0 deletions lib/y2024/day18.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule Y2024.Day18 do
use Advent.Day, no: 18

alias Advent.PathGrid

def part1(bytes, size \\ 50, byte_num \\ 1024) do
fallen = Enum.take(bytes, byte_num)

graph =
Enum.reduce(fallen, empty_grid({0, 0}, {size, size}), fn coord, graph ->
PathGrid.add_wall(graph, coord)
end)

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

@doc """
iex> Day18.parse_input("5,4\\n4,2\\n4,5\\n3,0\\n")
[{5,4}, {4,2}, {4,5}, {3,0}]
"""
def parse_input(input) do
input
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
[left, right] = String.split(line, ",", parts: 2)
{String.to_integer(left), String.to_integer(right)}
end)
end

@doc """
Create a completely empty path grid of a given size.
"""
def empty_grid({from_row, from_col}, {to_row, to_col}) do
Enum.reduce(from_row..to_row, Graph.new(vertex_identifier: & &1), fn row, graph ->
Enum.reduce(from_col..to_col, graph, fn col, graph ->
graph = Graph.add_vertex(graph, {row, col}, :floor)

[{row - 1, col}, {row, col - 1}]
|> Enum.reduce(graph, fn neighbour, graph ->
if PathGrid.floor?(graph, neighbour) do
graph
|> Graph.add_edge({row, col}, neighbour)
|> Graph.add_edge(neighbour, {row, col})
else
graph
end
end)
end)
end)
end

def part1_verify, do: input() |> parse_input() |> part1(70, 1024)
# def part2_verify, do: input() |> parse_input() |> part2()
end
Binary file added lib/y2024/input/day18.txt
Binary file not shown.
14 changes: 14 additions & 0 deletions test/y2024/day18_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Y2024.Day18Test do
use ExUnit.Case, async: true
alias Y2024.Day18
doctest Day18

test "part1" do
assert test_data() |> Day18.parse_input() |> Day18.part1(6, 12) == 22
end

test "verification, part 1", do: assert(Day18.part1_verify() == 436)
# test "verification, part 2", do: assert(Day18.part2_verify() == "update or delete me")

def test_data(), do: File.read!("test/y2024/input/day18/sample.txt")
end
Binary file added test/y2024/input/day18/sample.txt
Binary file not shown.

0 comments on commit 7ef665a

Please sign in to comment.