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.

116 lines
3.4 KiB

  1. using System;
  2. using static System.Console;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using Xunit;
  8. namespace AdventOfCode {
  9. struct Contains {
  10. public Contains(string outer, string inner, int count) {
  11. Outer = outer;
  12. Inner = inner;
  13. Count = count;
  14. }
  15. public string Outer;
  16. public string Inner;
  17. public int Count;
  18. public override string ToString() {
  19. return $"{Outer} contains {Count} {Inner}";
  20. }
  21. }
  22. public class Day07 {
  23. static void AddRules(string rule, List<Contains> contains) {
  24. string[] tokens = rule.Split(" bags contain ");
  25. string color = tokens[0];
  26. string[] otherBags = tokens[1].Split(", ");
  27. foreach (var s in otherBags) {
  28. Match m = Regex.Match(s, @"^(\d+) (\w+ \w+) bag");
  29. if (!m.Success) {
  30. continue;
  31. }
  32. int count = int.Parse(m.Groups[1].Value);
  33. Contains c = new Contains(color, m.Groups[2].Value, count);
  34. contains.Add(c);
  35. }
  36. }
  37. static List<Contains> ParseRules(string[] rules) {
  38. var contains = new List<Contains>();
  39. rules.ToList().ForEach(rule => AddRules(rule, contains));
  40. return contains;
  41. }
  42. static int ComputePart1(List<Contains> contains) {
  43. int setSize = 0;
  44. var result = new HashSet<string>();
  45. result.Add("shiny gold");
  46. while (true) {
  47. foreach (Contains c in contains) {
  48. if (result.Contains(c.Inner)) {
  49. result.Add(c.Outer);
  50. }
  51. }
  52. if (result.Count() == setSize) { // nothing new to add, we're done
  53. return result.Count() - 1; // shiny gold doesn't contain itself
  54. }
  55. setSize = result.Count();
  56. }
  57. }
  58. static int CountBags(List<Contains> rules, string target) {
  59. int total = 1;
  60. foreach (Contains rule in rules) {
  61. if (rule.Outer == target) {
  62. total += rule.Count * CountBags(rules, rule.Inner);
  63. }
  64. }
  65. return total;
  66. }
  67. static int Part1() {
  68. List<Contains> contains = ParseRules(File.ReadAllLines(Util.RootDir + "day07.txt"));
  69. return ComputePart1(contains);
  70. }
  71. static int Part2() {
  72. List<Contains> contains = ParseRules(File.ReadAllLines(Util.RootDir + "day07.txt"));
  73. return CountBags(contains, "shiny gold") - 1;
  74. }
  75. [Fact]
  76. public static void Test() {
  77. string[] example =
  78. @"light red bags contain 1 bright white bag, 2 muted yellow bags.
  79. dark orange bags contain 3 bright white bags, 4 muted yellow bags.
  80. bright white bags contain 1 shiny gold bag.
  81. muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
  82. shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
  83. dark olive bags contain 3 faded blue bags, 4 dotted black bags.
  84. vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
  85. faded blue bags contain no other bags.
  86. dotted black bags contain no other bags.".Split('\n');
  87. Assert.Equal(4, ComputePart1(ParseRules(example)));
  88. Assert.Equal(213, Part1());
  89. string[] example2 =
  90. @"shiny gold bags contain 2 dark red bags.
  91. dark red bags contain 2 dark orange bags.
  92. dark orange bags contain 2 dark yellow bags.
  93. dark yellow bags contain 2 dark green bags.
  94. dark green bags contain 2 dark blue bags.
  95. dark blue bags contain 2 dark violet bags.
  96. dark violet bags contain no other bags.".Split('\n');
  97. Assert.Equal(127, CountBags(ParseRules(example2), "shiny gold"));
  98. Assert.Equal(38426, Part2());
  99. }
  100. }
  101. }