adventofcode/2020/Day07.cs

93 lines
2.5 KiB
C#

using System;
using static System.Console;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Xunit;
namespace AdventOfCode {
struct Contains {
public Contains(string outer, string inner, int count) {
Outer = outer;
Inner = inner;
Count = count;
}
public string Outer;
public string Inner;
public int Count;
public override string ToString() {
return $"{Outer} contains {Count} {Inner}";
}
}
public class Day07 {
static void AddRules(string rule, List<Contains> contains) {
string[] tokens = rule.Split(" bags contain ");
string color = tokens[0];
string[] otherBags = tokens[1].Split(", ");
foreach (var s in otherBags) {
Match m = Regex.Match(s, @"^(\d+) (\w+ \w+) bag");
if (!m.Success) {
continue;
}
int count = int.Parse(m.Groups[1].Value);
Contains c = new Contains(color, m.Groups[2].Value, count);
contains.Add(c);
}
}
static int ParseRules(string[] rules) {
var contains = new List<Contains>();
rules.ToList().ForEach(rule => AddRules(rule, contains));
int setSize = 0;
var result = new HashSet<string>();
result.Add("shiny gold");
while (true) {
foreach (Contains c in contains) {
if (result.Contains(c.Inner)) {
result.Add(c.Outer);
}
}
if (result.Count() == setSize) { // nothing new to add, we're done
return result.Count() - 1; // shiny gold doesn't contain itself
}
setSize = result.Count();
}
}
static int Part1() {
string[] input = File.ReadAllLines(Util.RootDir + "day07.txt");
return ParseRules(input);
}
static int Part2() {
return -1;
}
[Fact]
public static void Test() {
string[] example =
@"light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
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, ParseRules(example));
Assert.Equal(213, Part1());
Assert.Equal(-1, Part2());
}
}
}