AdventOfCode/day7/day7part1.exs

55 lines
1.5 KiB
Elixir
Raw Normal View History

2020-12-07 06:29:07 +00:00
defmodule Day7Part1 do
@bag_spec ~r/\A(\d) (\w+ \w+).*\z/
def run do
File.read!("input")
|> input_to_tree()
|> invert_tree()
|> count_ancestors("shiny gold")
|> IO.puts()
end
def count_ancestors(itree, name), do: count_ancestors([name], MapSet.new(), itree)
def count_ancestors([], bags, _itree), do: Enum.count(bags)
def count_ancestors([name | rest], bags, itree) do
case Map.fetch(itree, name) do
:error -> count_ancestors(rest, bags, itree)
{:ok, names} -> count_ancestors(names ++ rest, MapSet.union(bags, MapSet.new(names)), itree)
end
end
def invert_tree(tree) do
Enum.reduce(tree, %{}, fn {parent, children}, inverted_tree ->
Enum.reduce(children, inverted_tree, fn {_n, child}, inverted_tree ->
Map.update(inverted_tree, child, [parent], &[parent | &1])
end)
end)
end
def input_to_tree(input) do
input
|> String.trim(".\n")
|> String.split(".\n")
|> Enum.map(&String.split(&1, " bags contain "))
|> Map.new(fn [parent, child_spec] -> {parent, parse_children(child_spec)} end)
end
def parse_children(child_spec) when is_binary(child_spec) do
child_spec
|> String.split(", ")
|> parse_children()
end
def parse_children(["no other bags"]), do: []
def parse_children(list) do
list
|> Enum.map(&Regex.run(@bag_spec, &1, capture: :all_but_first))
|> Enum.map(fn [count, type] -> {String.to_integer(count), type} end)
end
end
Day7Part1.run()