1
0
Fork 0

solve 2022 day 7 pt 2

This commit is contained in:
Sloane Perrault 2022-12-10 10:47:37 -05:00
parent fc2c674f0f
commit f59eb92cc7
No known key found for this signature in database
2 changed files with 27 additions and 7 deletions

View file

@ -2,6 +2,32 @@ import AOC
aoc 2022, 7 do aoc 2022, 7 do
def p1(input) 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 {sized_dirs, _} = input
|> String.split("\n") |> String.split("\n")
|> Enum.flat_map(&transform_input_line/1) |> Enum.flat_map(&transform_input_line/1)
@ -10,12 +36,6 @@ aoc 2022, 7 do
|> calculate_dir_size("/") |> calculate_dir_size("/")
sized_dirs sized_dirs
|> Enum.map(&elem(&1, 1))
|> Enum.reject(& &1 > 100_000)
|> Enum.sum()
end
def p2(_input) do
end end
defp transform_input_line("$ cd " <> path), do: [{:cd, path}] defp transform_input_line("$ cd " <> path), do: [{:cd, path}]

View file

@ -13,7 +13,7 @@
1. 2019 1. 2019
1. [2020] **17/50** 🌟 1. [2020] **17/50** 🌟
1. [2021] **43/50** 🌟 1. [2021] **43/50** 🌟
1. [2022] **13/50** 🌟 1. [2022] **14/50** 🌟
[2015]: ./2015 [2015]: ./2015
[2017]: ./2017 [2017]: ./2017