diff --git a/2022/elixir/lib/2022/7.ex b/2022/elixir/lib/2022/7.ex index 33913a5..52e5f1d 100644 --- a/2022/elixir/lib/2022/7.ex +++ b/2022/elixir/lib/2022/7.ex @@ -2,6 +2,32 @@ import AOC aoc 2022, 7 do def p1(input) do + input + |> get_sized_dirs() + |> Enum.map(&elem(&1, 1)) + |> Enum.reject(& &1 > 100_000) + |> Enum.sum() + end + + def p2(input) do + fs_size = 70_000_000 + required_free_space = 30_000_000 + + sized_dirs = get_sized_dirs(input) + + total_used = sized_dirs["/"] + + available_free_space = fs_size - total_used + + need_to_delete_min = required_free_space - available_free_space + + sized_dirs + |> Enum.map(&elem(&1, 1)) + |> Enum.reject(& &1 < need_to_delete_min) + |> Enum.min() + end + + defp get_sized_dirs(input) do {sized_dirs, _} = input |> String.split("\n") |> Enum.flat_map(&transform_input_line/1) @@ -10,12 +36,6 @@ aoc 2022, 7 do |> calculate_dir_size("/") sized_dirs - |> Enum.map(&elem(&1, 1)) - |> Enum.reject(& &1 > 100_000) - |> Enum.sum() - end - - def p2(_input) do end defp transform_input_line("$ cd " <> path), do: [{:cd, path}] diff --git a/README.md b/README.md index 4c0430b..67d51f9 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ 1. 2019 1. [2020] **17/50** 🌟 1. [2021] **43/50** 🌟 -1. [2022] **13/50** 🌟 +1. [2022] **14/50** 🌟 [2015]: ./2015 [2017]: ./2017