Skip to content

Commit

Permalink
2024 day 20, part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenseacat committed Dec 20, 2024
1 parent da723fa commit 9767ae3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 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=456%20stars&style=for-the-badge&color=green" alt="456 stars" /></p>
<p><a href="./lib/y2024/"><img src="https://img.shields.io/static/v1?label=2024&message=37%20stars&style=for-the-badge&color=yellow" alt="37 stars" /></a><br />
<p><img src="https://img.shields.io/static/v1?label=Total&message=457%20stars&style=for-the-badge&color=green" alt="457 stars" /></p>
<p><a href="./lib/y2024/"><img src="https://img.shields.io/static/v1?label=2024&message=38%20stars&style=for-the-badge&color=yellow" alt="38 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
5 changes: 3 additions & 2 deletions 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=37%20stars&style=for-the-badge&color=yellow" alt="37 stars" /><!-- stars 2024 end -->
<!-- stars 2024 start --><img src="https://img.shields.io/static/v1?label=2024&message=38%20stars&style=for-the-badge&color=yellow" alt="38 stars" /><!-- stars 2024 end -->

## Benchmarks

Expand Down Expand Up @@ -50,5 +50,6 @@ day 18, part 1 22.53 44.38 ms ±5.11% 44.48 ms 48.
day 18, part 2 4.04 247.58 ms ±3.18% 247.68 ms 260.90 ms
day 19, part 1 29.38 34.03 ms ±0.86% 34.01 ms 35.47 ms
day 19, part 2 29.36 34.06 ms ±1.18% 34.03 ms 36.73 ms
day 20, part 1 13.16 76.00 ms ±8.60% 75.01 ms 89.74 ms
day 20, part 1 13.45 74.33 ms ±6.84% 74.14 ms 83.83 ms
day 20, part 2 1.30 771.43 ms ±1.61% 767.13 ms 796.90 ms
```
39 changes: 15 additions & 24 deletions lib/y2024/day20.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@ defmodule Y2024.Day20 do

alias Advent.PathGrid

def part1(input) do
def parts(input, cheat_size) do
input
|> cheats
|> cheats(cheat_size)
|> Enum.reduce(0, fn {saving, count}, acc ->
if saving >= 100, do: acc + count, else: acc
end)
end

# @doc """
# iex> Day20.part2("update or delete me")
# "update or delete me"
# """
# def part2(input) do
# input
# end

def cheats({graph, from, to}) do
def cheats({graph, from, to}, max_cheat_size) do
# The default path through the grid takes *every floor space*. Use this
# to our advantage.
baseline = Graph.get_shortest_path(graph, from, to)
Expand All @@ -29,22 +21,21 @@ defmodule Y2024.Day20 do
|> Enum.with_index()
|> Map.new()

cheat_options =
for row <- -max_cheat_size..max_cheat_size,
col <- -max_cheat_size..max_cheat_size,
abs(row) + abs(col) <= max_cheat_size,
do: {row, col}

# For each step in the path, see what happens if we knock out walls around it
# and skip ahead in the path
Enum.flat_map(baseline, fn {row, col} ->
from_index = Map.get(map, {row, col})

Enum.map([{0, -1}, {0, 1}, {-1, 0}, {1, 0}], fn {o_row, o_col} ->
to_index = Map.get(map, {row + o_row * 2, col + o_col * 2})
maybe_wall = {row + o_row, col + o_col}
Enum.map(cheat_options, fn {o_row, o_col} ->
to_index = Map.get(map, {row + o_row, col + o_col})

cond do
!PathGrid.in_graph?(graph, maybe_wall) ->
nil

PathGrid.floor?(graph, maybe_wall) ->
nil

to_index == nil ->
nil

Expand All @@ -53,10 +44,10 @@ defmodule Y2024.Day20 do

true ->
# This will be the saving of time - 2 is the length of the shortcut
to_index - from_index - 2
to_index - from_index - abs(o_row) - abs(o_col)
end
end)
|> Enum.reject(&(&1 == nil))
|> Enum.reject(&(&1 == nil || &1 == 0))
end)
|> Enum.frequencies()
end
Expand All @@ -69,6 +60,6 @@ defmodule Y2024.Day20 do
{grid.graph, from, to}
end

def part1_verify, do: input() |> parse_input() |> part1()
# def part2_verify, do: input() |> parse_input() |> part2()
def part1_verify, do: input() |> parse_input() |> parts(2)
def part2_verify, do: input() |> parse_input() |> parts(20)
end
4 changes: 2 additions & 2 deletions test/y2024/day20_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ defmodule Y2024.Day20Test do
64 => 1
}

assert Day20.parse_input(@sample) |> Day20.cheats() == expected
assert Day20.parse_input(@sample) |> Day20.cheats(2) == expected
end

test "verification, part 1", do: assert(Day20.part1_verify() == 1289)
# test "verification, part 2", do: assert(Day20.part2_verify() == "update or delete me")
test "verification, part 2", do: assert(Day20.part2_verify() == 982_425)
end

0 comments on commit 9767ae3

Please sign in to comment.