small simplification

This commit is contained in:
Adam Millerchip 2020-12-08 12:39:29 +09:00
parent dbf14680ba
commit c6a64056c6
3 changed files with 7 additions and 17 deletions

View File

@ -9,7 +9,7 @@ $ elixir day7part1.exs
Thoughts:
First parse the input to a map of node => children, reprensenting requirement tree.
First parse the input to a map of node => children, representing the requirement tree.
The question is to find the ancestors, so invert the tree.
Use MapSet to avoid duplicates.
Don't need the child count, but keep it around just in case.

View File

@ -35,16 +35,11 @@ defmodule Day7Part1 do
|> Map.new(fn [parent, child_spec] -> {parent, parse_children(child_spec)} end)
end
def parse_children(child_spec) when is_binary(child_spec) do
def parse_children("no other bags"), do: []
def parse_children(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

View File

@ -21,16 +21,11 @@ defmodule Day7Part2 do
|> Map.new(fn [parent, child_spec] -> {parent, parse_children(child_spec)} end)
end
def parse_children(child_spec) when is_binary(child_spec) do
def parse_children("no other bags"), do: []
def parse_children(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