From ac02740e85877cecd476efca1f39ad4f6503860e Mon Sep 17 00:00:00 2001 From: sloane Date: Wed, 11 Dec 2024 09:03:05 -0500 Subject: [PATCH] solve 2024 day 10 --- 2024/README.md | 3 +- 2024/lib/2024/10.ex | 84 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 4 +-- 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 2024/lib/2024/10.ex diff --git a/2024/README.md b/2024/README.md index 316419d..12a9c72 100644 --- a/2024/README.md +++ b/2024/README.md @@ -3,7 +3,7 @@ | S | M | T | W | T | F | S | | :-: | :-: | :-: | :-: | :-: | :-: | :-: | | [1] | [2] | [3] | [4] | [5] | [6] | [7] | -| [8] | [9] | 10 | 11 | 12 | 13 | 14 | +| [8] | [9] | [10]| 11 | 12 | 13 | 14 | | 15 | 16 | 17 | 18 | 19 | 20 | 21 | | 22 | 23 | 24 | 25 | | | | @@ -16,3 +16,4 @@ [7]: ./lib/2024/7.ex [8]: ./lib/2024/8.ex [9]: ./lib/2024/9.ex +[10]: ./lib/2024/10.ex diff --git a/2024/lib/2024/10.ex b/2024/lib/2024/10.ex new file mode 100644 index 0000000..caaaadd --- /dev/null +++ b/2024/lib/2024/10.ex @@ -0,0 +1,84 @@ +import AOC +import AOC.Prelude + +aoc 2024, 10 do + def p1(input) do + map = read_map(input) + + trailheads = map |> Map.filter(&match?({_, 0}, &1)) |> Map.keys() + + trailheads + |> Enum.map(&score(&1, map)) + |> Enum.sum() + end + + def p2(input) do + map = read_map(input) + + trailheads = map |> Map.filter(&match?({_, 0}, &1)) |> Map.keys() + + trailheads + |> Enum.map(&rating(&1, map)) + |> Enum.sum() + end + + ## part 1 + + defp score(pos, map) do + pos + |> reachable_nines(map, MapSet.new()) + |> Enum.count() + end + + defp reachable_nines(pos, map, seen) do + seen = MapSet.put(seen, pos) + curr = Map.get(map, pos) + + if curr == 9 do + [pos] + else + [&n/1, &s/1, &e/1, &w/1] + |> Enum.map(& &1.(pos)) + |> Enum.reject(&MapSet.member?(seen, &1)) + |> Enum.filter(&(Map.get(map, &1, 0) == curr + 1)) + |> Enum.flat_map(&reachable_nines(&1, map, seen)) + |> Enum.uniq() + end + end + + ## part 2 + + defp rating(pos, map, seen \\ MapSet.new()) do + seen = MapSet.put(seen, pos) + curr = Map.get(map, pos) + + if curr == 9 do + 1 + else + [&n/1, &s/1, &e/1, &w/1] + |> Enum.map(& &1.(pos)) + |> Enum.reject(&MapSet.member?(seen, &1)) + |> Enum.filter(&(Map.get(map, &1, 0) == curr + 1)) + |> Enum.map(&rating(&1, map, seen)) + |> Enum.sum() + end + end + + ## shared + + defp n({x, y}), do: {x, y + 1} + defp s({x, y}), do: {x, y - 1} + defp e({x, y}), do: {x + 1, y} + defp w({x, y}), do: {x - 1, y} + + ## input + + def read_map(input) do + input + |> map_grid() + |> Enum.reject(&match?({_, "."}, &1)) + |> Map.new(fn + {k, v} -> {k, String.to_integer(v)} + end) + end +end diff --git a/README.md b/README.md index b3b4d49..1ed3b9c 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ | [2021] | **43/50** 🌟 | Elixir | | [2022] | **14/50** 🌟 | Elixir, Haskell | | [2023] | **19/50** 🌟 | Elixir, Haskell | -| [2024] | **18/50** 🌟 | Elixir | -| **Total** | **174** 🌟| | +| [2024] | **20/50** 🌟 | Elixir | +| **Total** | **176** 🌟| | [2015]: ./2015 [2017]: ./2017