From 4879d7a7d2fd9c298bc5ca9a87bb37ccc555d82b Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Tue, 10 Dec 2024 17:15:25 +0900 Subject: [PATCH] Simplify 2024 Day 10 to remove MapSets --- 2024/day10.exs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/2024/day10.exs b/2024/day10.exs index 76d4d22..9733ce7 100644 --- a/2024/day10.exs +++ b/2024/day10.exs @@ -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