-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I love when part 2 just falls out from doing part 1
- Loading branch information
1 parent
5138af9
commit 72d293d
Showing
6 changed files
with
152 additions
and
3 deletions.
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
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,67 @@ | ||
defmodule Y2024.Day10 do | ||
use Advent.Day, no: 10 | ||
|
||
alias Advent.Grid | ||
|
||
def part1({grid, path_grid}) do | ||
destinations = Enum.filter(grid, &destination?/1) | ||
|
||
grid | ||
|> Enum.filter(&trailhead?/1) | ||
|> Task.async_stream(&find_trailhead_score(&1, path_grid, destinations)) | ||
|> Enum.reduce(0, fn {:ok, num}, acc -> acc + num end) | ||
end | ||
|
||
def part2({grid, path_grid}) do | ||
destinations = Enum.filter(grid, &destination?/1) | ||
|
||
grid | ||
|> Enum.filter(&trailhead?/1) | ||
|> Task.async_stream(&find_trailhead_rating(&1, path_grid, destinations)) | ||
|> Enum.reduce(0, fn {:ok, num}, acc -> acc + num end) | ||
end | ||
|
||
def parse_input(input) do | ||
grid = input |> Grid.new() |> Grid.use_number_values() | ||
{grid, to_path_grid(grid)} | ||
end | ||
|
||
defp to_path_grid(grid) do | ||
Enum.reduce(Map.keys(grid), Graph.new(vertex_identifier: & &1), fn from, graph -> | ||
Enum.reduce(find_valid_movements(grid, from), graph, fn to, graph -> | ||
graph | ||
|> Graph.add_vertices([from, to]) | ||
|> Graph.add_edge(from, to) | ||
end) | ||
end) | ||
end | ||
|
||
defp trailhead?({_coord, val}), do: val == 0 | ||
defp destination?({_coord, val}), do: val == 9 | ||
|
||
defp find_trailhead_score({coord, _}, path_grid, destinations) do | ||
Enum.count(destinations, fn {dest_coord, _} -> | ||
Graph.get_shortest_path(path_grid, coord, dest_coord) != nil | ||
end) | ||
end | ||
|
||
defp find_trailhead_rating({coord, _}, path_grid, destinations) do | ||
Enum.reduce(destinations, 0, fn {dest_coord, _}, count -> | ||
count + (Graph.get_paths(path_grid, coord, dest_coord) |> length) | ||
end) | ||
end | ||
|
||
defp find_valid_movements(map, {row, col}) do | ||
case Map.get(map, {row, col}) do | ||
"." -> | ||
[] | ||
|
||
value -> | ||
[{row + 1, col}, {row - 1, col}, {row, col - 1}, {row, col + 1}] | ||
|> Enum.filter(fn coord -> Map.get(map, coord) == value + 1 end) | ||
end | ||
end | ||
|
||
def part1_verify, do: input() |> parse_input() |> part1() | ||
def part2_verify, do: input() |> parse_input() |> part2() | ||
end |
Binary file not shown.
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,67 @@ | ||
defmodule Y2024.Day10Test do | ||
use ExUnit.Case, async: true | ||
alias Y2024.Day10 | ||
doctest Day10 | ||
|
||
describe "part 1" do | ||
test "sample 1" do | ||
input = """ | ||
...0... | ||
...1... | ||
...2... | ||
6543456 | ||
7.....7 | ||
8.....8 | ||
9.....9 | ||
""" | ||
|
||
assert Day10.parse_input(input) |> Day10.part1() == 2 | ||
end | ||
|
||
test "sample 2" do | ||
input = """ | ||
..90..9 | ||
...1.98 | ||
...2..7 | ||
6543456 | ||
765.987 | ||
876.... | ||
987.... | ||
""" | ||
|
||
assert Day10.parse_input(input) |> Day10.part1() == 4 | ||
end | ||
|
||
test "sample 3" do | ||
input = """ | ||
10..9.. | ||
2...8.. | ||
3...7.. | ||
4567654 | ||
...8..3 | ||
...9..2 | ||
.....01 | ||
""" | ||
|
||
assert Day10.parse_input(input) |> Day10.part1() == 3 | ||
end | ||
|
||
test "sample 4" do | ||
input = """ | ||
89010123 | ||
78121874 | ||
87430965 | ||
96549874 | ||
45678903 | ||
32019012 | ||
01329801 | ||
10456732 | ||
""" | ||
|
||
assert Day10.parse_input(input) |> Day10.part1() == 36 | ||
end | ||
end | ||
|
||
test "verification, part 1", do: assert(Day10.part1_verify() == 638) | ||
test "verification, part 2", do: assert(Day10.part2_verify() == 1289) | ||
end |