solve 2024 day 10
This commit is contained in:
parent
d8a95aaae3
commit
ac02740e85
3 changed files with 88 additions and 3 deletions
|
@ -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
|
||||
|
|
84
2024/lib/2024/10.ex
Normal file
84
2024/lib/2024/10.ex
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue