Tidy up Day 7

This commit is contained in:
Adam Millerchip 2022-12-08 01:54:57 +09:00
parent 88a64017fc
commit 87331c5a5a

View file

@ -1,74 +1,79 @@
defmodule Day7 do defmodule Day7 do
@root ["/"] @root ["/"]
@total_size 70_000_000
@space_needed 30_000_000
def part1(input) do def part1(input) do
store = build_tree(input) input
|> filetree_with_dir_sizes()
[@root]
|> build_queue([], store)
|> Enum.reduce(store, &update_dir_size/2)
|> Enum.filter(fn |> Enum.filter(fn
{_k, {:dir, size, _paths}} -> size <= 100_000 {_k, {:dir, size, _paths}} -> size <= 100_000
_ -> false {_k, {:file, _size, _paths}} -> false
end) end)
|> Enum.map(fn {_k, {_type, size, _paths}} -> size end) |> Enum.map(fn {_k, {_type, size, _paths}} -> size end)
|> Enum.sum() |> Enum.sum()
end end
def update_dir_size(path, store) do defp filetree_with_dir_sizes(input) do
{:dir, 0, paths} = Map.get(store, path) tree = build_tree(input)
size = store |> Map.take(paths) |> Enum.map(fn {_, {_, size, _}} -> size end) |> Enum.sum()
Map.put(store, path, {:dir, size, paths}) tree
|> dirs_fewest_descendents_first()
|> Enum.reduce(tree, &put_dir_size/2)
end end
def build_queue([], queue, _store), do: queue defp put_dir_size(path, tree) do
{:dir, nil, paths} = Map.get(tree, path)
size = tree |> Map.take(paths) |> Enum.map(fn {_, {_, size, _}} -> size end) |> Enum.sum()
Map.put(tree, path, {:dir, size, paths})
end
def build_queue([node | rest], queue, store) do defp dirs_fewest_descendents_first(tree, to_check \\ [@root], found_dirs \\ [])
case Map.get(store, node) do defp dirs_fewest_descendents_first(_tree, [], found_dirs), do: found_dirs
{:file, _size, nil} -> build_queue(rest, queue, store)
{:dir, _size, paths} -> build_queue(paths ++ rest, [node | queue], store) defp dirs_fewest_descendents_first(tree, [node | rest], dirs) do
case Map.get(tree, node) do
{:file, _size, nil} -> dirs_fewest_descendents_first(tree, rest, dirs)
{:dir, _size, paths} -> dirs_fewest_descendents_first(tree, paths ++ rest, [node | dirs])
end end
end end
def build_tree(input, path \\ [], store \\ %{}) defp build_tree(input, path \\ [], tree \\ %{})
def build_tree([], _path, store), do: store defp build_tree([], _path, tree), do: tree
def build_tree([{:cd, "/"} | rest], _path, store), do: build_tree(rest, @root, store) defp build_tree([{:cd, "/"} | rest], _path, tree), do: build_tree(rest, @root, tree)
def build_tree([{:cd, ".."} | rest], [_here | path], store), do: build_tree(rest, path, store) defp build_tree([{:cd, ".."} | rest], [_here | path], tree), do: build_tree(rest, path, tree)
def build_tree([{:cd, dir} | rest], path, store), do: build_tree(rest, [dir | path], store) defp build_tree([{:cd, dir} | rest], path, tree), do: build_tree(rest, [dir | path], tree)
def build_tree([{:ls, items} | rest], path, store) do defp build_tree([{:ls, items} | rest], path, tree) do
# insert files into store with directory path {paths, tree} =
{paths, store} = Enum.map_reduce(items, tree, fn
Enum.map_reduce(items, store, fn # Create an empty directory for each dir listed
{:dir, name}, store -> # (needed if we we forget to ls inside a dir)
{:dir, nil, name}, tree ->
item_path = [name | path] item_path = [name | path]
{item_path, Map.put(store, item_path, {:dir, 0, []})} {item_path, Map.put(tree, item_path, {:dir, nil, []})}
{:file, size, name}, store -> # Create a file for each file listed
{:file, size, name}, tree ->
item_path = [name | path] item_path = [name | path]
{item_path, Map.put(store, item_path, {:file, size, nil})} {item_path, Map.put(tree, item_path, {:file, size, nil})}
end) end)
store = Map.put(store, path, {:dir, 0, paths}) # Create current directory with listed children
tree = Map.put(tree, path, {:dir, nil, paths})
build_tree(rest, path, store) build_tree(rest, path, tree)
end end
@total_size 70_000_000
@space_needed 30_000_000
def part2(input) do def part2(input) do
store = build_tree(input) tree = filetree_with_dir_sizes(input)
store = {:dir, space_used, _} = Map.get(tree, @root)
[@root]
|> build_queue([], store)
|> Enum.reduce(store, &update_dir_size/2)
{:dir, space_used, _} = Map.get(store, @root)
unused_space = @total_size - space_used unused_space = @total_size - space_used
space_to_free = @space_needed - unused_space space_to_free = @space_needed - unused_space
store tree
|> Enum.filter(&match?({_path, {:dir, _size, _paths}}, &1)) |> Enum.filter(&match?({_path, {:dir, _size, _paths}}, &1))
|> Enum.map(fn {_path, {:dir, size, _paths}} -> size end) |> Enum.map(fn {_path, {:dir, size, _paths}} -> size end)
|> Enum.sort() |> Enum.sort()
@ -87,16 +92,16 @@ defmodule Day7 do
end end
end end
def parse_input([]), do: [] defp parse_input([]), do: []
def parse_input(["$ cd " <> dir | rest]), do: [{:cd, dir} | parse_input(rest)] defp parse_input(["$ cd " <> dir | rest]), do: [{:cd, dir} | parse_input(rest)]
def parse_input(["$ ls" | rest]) do defp parse_input(["$ ls" | rest]) do
{listings, rest} = Enum.split_while(rest, &(!match?("$" <> _, &1))) {listings, rest} = Enum.split_while(rest, &(!match?("$" <> _, &1)))
listings = listings =
Enum.map(listings, fn Enum.map(listings, fn
"dir " <> dir -> "dir " <> dir ->
{:dir, dir} {:dir, nil, dir}
file -> file ->
[size, name] = String.split(file) [size, name] = String.split(file)