From c6a64056c65d0eace556a3aa68d321dd19c00694 Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Tue, 8 Dec 2020 12:39:29 +0900 Subject: [PATCH] small simplification --- day7/README | 2 +- day7/day7part1.exs | 11 +++-------- day7/day7part2.exs | 11 +++-------- 3 files changed, 7 insertions(+), 17 deletions(-) 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