diff --git a/2020/Day07.cs b/2020/Day07.cs index d7bffa8..156a9ff 100644 --- a/2020/Day07.cs +++ b/2020/Day07.cs @@ -46,11 +46,12 @@ namespace AdventOfCode { return contains; } - static int ComputePart1(List contains) { - int setSize = 0; + static int WhatCanContain(List contains, string target) { var result = new HashSet(); - result.Add("shiny gold"); + int setSize = 0; + result.Add(target); + // transitive closure: keep adding things, until a round of doing so doesn't add anything new while (true) { foreach (Contains c in contains) { if (result.Contains(c.Inner)) { @@ -58,7 +59,7 @@ namespace AdventOfCode { } } if (result.Count() == setSize) { // nothing new to add, we're done - return result.Count() - 1; // shiny gold doesn't contain itself + return result.Count() - 1; // our target doesn't actually contain itself } setSize = result.Count(); } @@ -76,7 +77,7 @@ namespace AdventOfCode { static int Part1() { List contains = ParseRules(File.ReadAllLines(Util.RootDir + "day07.txt")); - return ComputePart1(contains); + return WhatCanContain(contains, "shiny gold"); } static int Part2() { @@ -97,7 +98,7 @@ vibrant plum bags contain 5 faded blue bags, 6 dotted black bags. faded blue bags contain no other bags. dotted black bags contain no other bags.".Split('\n'); - Assert.Equal(4, ComputePart1(ParseRules(example))); + Assert.Equal(4, WhatCanContain(ParseRules(example), "shiny gold")); Assert.Equal(213, Part1()); string[] example2 =