diff --git a/day7/README b/day7/README index 663bb6b..0d11bfa 100644 --- a/day7/README +++ b/day7/README @@ -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. diff --git a/day7/day7part1.exs b/day7/day7part1.exs index 39291cb..0f151d4 100644 --- a/day7/day7part1.exs +++ b/day7/day7part1.exs @@ -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 diff --git a/day7/day7part2.exs b/day7/day7part2.exs index fdeaf64..e87d278 100644 --- a/day7/day7part2.exs +++ b/day7/day7part2.exs @@ -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