Simplify 2024 Day 10 to remove MapSets

This commit is contained in:
Adam Millerchip 2024-12-10 17:15:25 +09:00
parent e4b27c9bc1
commit 4879d7a7d2

View file

@ -2,16 +2,15 @@
defmodule Day10 do
def part1({grid, zeros}) do
zeros
|> Enum.map(fn point -> MapSet.size(score(point, grid, 0)) end)
|> Enum.map(fn point -> point |> score(grid, 0) |> Enum.uniq() |> Enum.count() end)
|> Enum.sum()
end
def score(point, _grid, 9), do: MapSet.new([point])
def score(point, _grid, 9), do: [point]
def score({x, y}, grid, height) do
find_neighbours(x, y, height, grid)
|> Enum.map(fn {point, _} -> score(point, grid, height + 1) end)
|> Enum.reduce(MapSet.new(), &MapSet.union/2)
|> Enum.flat_map(fn {point, _} -> score(point, grid, height + 1) end)
end
def find_neighbours(x, y, height, grid) do
@ -24,15 +23,7 @@ defmodule Day10 do
def part2({grid, zeros}) do
zeros
|> Enum.map(fn point -> score2(point, grid, 0) end)
|> Enum.sum()
end
def score2(_point, _grid, 9), do: 1
def score2({x, y}, grid, height) do
find_neighbours(x, y, height, grid)
|> Enum.map(fn {point, _} -> score2(point, grid, height + 1) end)
|> Enum.map(fn point -> point |> score(grid, 0) |> Enum.count() end)
|> Enum.sum()
end