|
@ -46,11 +46,12 @@ namespace AdventOfCode { |
|
|
return contains; |
|
|
return contains; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static int ComputePart1(List<Contains> contains) { |
|
|
|
|
|
int setSize = 0; |
|
|
|
|
|
|
|
|
static int WhatCanContain(List<Contains> contains, string target) { |
|
|
var result = new HashSet<string>(); |
|
|
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) { |
|
|
while (true) { |
|
|
foreach (Contains c in contains) { |
|
|
foreach (Contains c in contains) { |
|
|
if (result.Contains(c.Inner)) { |
|
|
if (result.Contains(c.Inner)) { |
|
@ -58,7 +59,7 @@ namespace AdventOfCode { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if (result.Count() == setSize) { // nothing new to add, we're done
|
|
|
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(); |
|
|
setSize = result.Count(); |
|
|
} |
|
|
} |
|
@ -76,7 +77,7 @@ namespace AdventOfCode { |
|
|
|
|
|
|
|
|
static int Part1() { |
|
|
static int Part1() { |
|
|
List<Contains> contains = ParseRules(File.ReadAllLines(Util.RootDir + "day07.txt")); |
|
|
List<Contains> contains = ParseRules(File.ReadAllLines(Util.RootDir + "day07.txt")); |
|
|
return ComputePart1(contains); |
|
|
|
|
|
|
|
|
return WhatCanContain(contains, "shiny gold"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static int Part2() { |
|
|
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. |
|
|
faded blue bags contain no other bags. |
|
|
dotted black bags contain no other bags.".Split('\n');
|
|
|
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()); |
|
|
Assert.Equal(213, Part1()); |
|
|
|
|
|
|
|
|
string[] example2 = |
|
|
string[] example2 = |
|
|