You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.7 KiB

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 List<Contains> ParseRules(string[] rules) {
var contains = new List<Contains>();
rules.ToList().ForEach(rule => AddRules(rule, contains));
return contains;
}
static int ComputePart1(List<Contains> 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() {
List<Contains> contains = ParseRules(File.ReadAllLines(Util.RootDir + "day07.txt"));
return ComputePart1(contains);
}
static int Part2() {
List<Contains> contains = ParseRules(File.ReadAllLines(Util.RootDir + "day07.txt"));
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, ComputePart1(ParseRules(example)));
Assert.Equal(213, Part1());
Assert.Equal(-1, Part2());
}
}
}