Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #load "xunit"
- #load "AOC Connector"
- #load "AOC 2d Array"
- void Main()
- {
- RunTests(); // Call RunTests() or press Alt+Shift+T to initiate testing.
- //get aoc data
- var aoc = new AdventOfCode(2020, 7);
- Bag.GetAll(aoc.InputLines);
- //solve A
- aoc.SubmitAnswer(SolveA(), Part.A);
- //solve B
- aoc.SubmitAnswer(SolveB(), Part.B);
- }
- public int SolveA(string[] input = null)
- {
- //parse
- if(input != null) Bag.GetAll(input);
- //get golden bag
- var goldenBag = Bag.dicAllBags[("shiny", "gold")];
- //determine answer
- return Bag.dicAllBags.Values.Where(x=> x.HasBag(goldenBag)).Count();
- }
- public int SolveB(string[] input = null)
- {
- //parse
- if(input != null) Bag.GetAll(input);
- //determine answer
- return Bag.dicAllBags[("shiny", "gold")].SumBag();
- }
- class Bag
- {
- public string color;
- public string style;
- public Dictionary<Bag, int> dicContent = new Dictionary<UserQuery.Bag, int>();
- public static Dictionary<(string style, string color), Bag> dicAllBags = new Dictionary<(string, string), Bag>();
- public static Bag[] GetAll(string[] lines)
- {
- //remove possible test data
- dicAllBags.Clear();
- //parse each line
- string color, style;
- foreach (var line in lines)
- {
- //get this bag
- var m1 = Regex.Match(line, @"(\w+) (\w+) bags contain");
- style = m1.Groups[1].Value;
- color = m1.Groups[2].Value;
- var bag = FindCreate(style, color);
- //parse the content of the insides
- var m2 = Regex.Matches(line, @"(\d+) (\w+) (\w+) bag");
- foreach (Match mp in m2)
- {
- var cnt = int.Parse(mp.Groups[1].Value);
- style = mp.Groups[2].Value;
- color = mp.Groups[3].Value;
- //find existing bag
- var innerBag = FindCreate(style, color);
- bag.dicContent.Add(innerBag, cnt);
- }
- }
- return dicAllBags.Values.ToArray();
- }
- private static Bag FindCreate(string style, string color)
- {
- //find the bag that is part of this
- if (dicAllBags.TryGetValue((style, color), out Bag bag) == false)
- {
- bag = new Bag()
- {
- style = style,
- color = color
- };
- dicAllBags.Add((style, color), bag);
- }
- return bag;
- }
- public bool HasBag(Bag bag)
- {
- //check if this bag has bag x
- if(this.dicContent.ContainsKey(bag)) return true;
- //check the bags inside this bag
- foreach (var inner in this.dicContent)
- {
- if(inner.Key.HasBag(bag)) return true;
- }
- return false;//nothing found
- }
- public int SumBag()
- {
- int sum = 0;
- foreach (var innerBag in this.dicContent)
- {
- sum += innerBag.Value; //this inner bag count
- sum += innerBag.Key.SumBag() * innerBag.Value; //the bags inside
- }
- return sum;
- }
- }
- #region private::Tests
- #region test data
- string[] testData = new []
- {
- "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.",
- };
- string[] testdata2 = new []
- {
- "shiny gold bags contain 2 dark red bags.",
- "dark red bags contain 2 dark orange bags.",
- "dark orange bags contain 2 dark yellow bags.",
- "dark yellow bags contain 2 dark green bags.",
- "dark green bags contain 2 dark blue bags.",
- "dark blue bags contain 2 dark violet bags.",
- "dark violet bags contain no other bags.",
- };
- #endregion
- //tests
- [Fact] void TestA1() => Assert.Equal(004, SolveA(testData));
- [Fact] void TestB1() => Assert.Equal(032, SolveB(testData));
- [Fact] void TestB2() => Assert.Equal(126, SolveB(testdata2));
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement