Browse Source

clean up day 7 a bit

main
Colin McMillen 3 years ago
parent
commit
13d5d6c9d1
  1. 13
      2020/Day07.cs

13
2020/Day07.cs

@ -46,11 +46,12 @@ namespace AdventOfCode {
return contains;
}
static int ComputePart1(List<Contains> contains) {
int setSize = 0;
static int WhatCanContain(List<Contains> contains, string target) {
var result = new HashSet<string>();
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> 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 =

Loading…
Cancel
Save