Advertisement
Equd

AOC 2020 Day 07

Dec 7th, 2020
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. #load "xunit"
  2. #load "AOC Connector"
  3. #load "AOC 2d Array"
  4.  
  5. void Main()
  6. {  
  7.     RunTests();  // Call RunTests() or press Alt+Shift+T to initiate testing.
  8.  
  9.     //get aoc data
  10.     var aoc = new AdventOfCode(2020, 7);       
  11.     Bag.GetAll(aoc.InputLines);
  12.    
  13.     //solve A  
  14.     aoc.SubmitAnswer(SolveA(), Part.A);
  15.    
  16.     //solve B
  17.     aoc.SubmitAnswer(SolveB(), Part.B);
  18. }
  19.  
  20. public int SolveA(string[] input = null)
  21. {
  22.     //parse
  23.     if(input != null) Bag.GetAll(input);   
  24.    
  25.     //get golden bag
  26.     var goldenBag = Bag.dicAllBags[("shiny", "gold")];
  27.    
  28.     //determine answer
  29.     return Bag.dicAllBags.Values.Where(x=> x.HasBag(goldenBag)).Count();
  30. }
  31.  
  32. public int SolveB(string[] input = null)
  33. {
  34.     //parse
  35.     if(input != null) Bag.GetAll(input);   
  36.    
  37.     //determine answer
  38.     return Bag.dicAllBags[("shiny", "gold")].SumBag();
  39. }
  40.  
  41. class Bag
  42. {
  43.     public string color;
  44.     public string style;
  45.     public Dictionary<Bag, int> dicContent = new Dictionary<UserQuery.Bag, int>();
  46.     public static Dictionary<(string style, string color), Bag> dicAllBags = new Dictionary<(string, string), Bag>();
  47.    
  48.     public static Bag[] GetAll(string[] lines)
  49.     {
  50.         //remove possible test data
  51.         dicAllBags.Clear();
  52.        
  53.         //parse each line
  54.         string color, style;
  55.         foreach (var line in lines)
  56.         {
  57.             //get this bag
  58.             var m1 = Regex.Match(line, @"(\w+) (\w+) bags contain");
  59.             style = m1.Groups[1].Value;
  60.             color = m1.Groups[2].Value;
  61.             var bag = FindCreate(style, color);
  62.  
  63.             //parse the content of the insides
  64.             var m2 = Regex.Matches(line, @"(\d+) (\w+) (\w+) bag");
  65.             foreach (Match mp in m2)
  66.             {
  67.                 var cnt = int.Parse(mp.Groups[1].Value);
  68.                 style = mp.Groups[2].Value;
  69.                 color = mp.Groups[3].Value;
  70.  
  71.                 //find existing bag
  72.                 var innerBag = FindCreate(style, color);
  73.                 bag.dicContent.Add(innerBag, cnt);
  74.             }
  75.         }
  76.         return dicAllBags.Values.ToArray();
  77.     }
  78.    
  79.     private static Bag FindCreate(string style, string color)
  80.     {
  81.         //find the bag that is part of this
  82.         if (dicAllBags.TryGetValue((style, color), out Bag bag) == false)
  83.         {
  84.             bag = new Bag()
  85.             {
  86.                 style = style,
  87.                 color = color
  88.             };
  89.             dicAllBags.Add((style, color), bag);
  90.         }
  91.         return bag;
  92.     }
  93.  
  94.     public bool HasBag(Bag bag)
  95.     {
  96.         //check if this bag has bag x
  97.         if(this.dicContent.ContainsKey(bag)) return true;
  98.        
  99.         //check the bags inside this bag
  100.         foreach (var inner in this.dicContent)
  101.         {
  102.             if(inner.Key.HasBag(bag)) return true;
  103.         }
  104.         return false;//nothing found
  105.     }
  106.    
  107.     public int SumBag()
  108.     {
  109.         int sum = 0;
  110.         foreach (var innerBag in this.dicContent)
  111.         {
  112.             sum += innerBag.Value; //this inner bag count
  113.             sum += innerBag.Key.SumBag() * innerBag.Value; //the bags inside
  114.         }
  115.         return sum;    
  116.     }
  117. }
  118.  
  119.  
  120. #region private::Tests
  121. #region test data
  122. string[] testData = new []
  123. {
  124. "light red bags contain 1 bright white bag, 2 muted yellow bags.",
  125. "dark orange bags contain 3 bright white bags, 4 muted yellow bags.",
  126. "bright white bags contain 1 shiny gold bag.",
  127. "muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.",
  128. "shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.",
  129. "dark olive bags contain 3 faded blue bags, 4 dotted black bags.",
  130. "vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.",
  131. "faded blue bags contain no other bags.",
  132. "dotted black bags contain no other bags.",
  133. };
  134.  
  135. string[] testdata2 = new []
  136. {
  137. "shiny gold bags contain 2 dark red bags.",
  138. "dark red bags contain 2 dark orange bags.",
  139. "dark orange bags contain 2 dark yellow bags.",
  140. "dark yellow bags contain 2 dark green bags.",
  141. "dark green bags contain 2 dark blue bags.",
  142. "dark blue bags contain 2 dark violet bags.",
  143. "dark violet bags contain no other bags.",
  144. };
  145.  
  146. #endregion
  147. //tests
  148. [Fact] void TestA1() => Assert.Equal(004, SolveA(testData));
  149. [Fact] void TestB1() => Assert.Equal(032,  SolveB(testData));
  150. [Fact] void TestB2() => Assert.Equal(126,  SolveB(testdata2));
  151. #endregion
  152.  
  153.  
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement