Program.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. namespace D15._2
  6. {
  7. public class Recipe
  8. {
  9. public Ingredient I;
  10. public (Recipe R, int Qt) R;
  11. }
  12. public class Ingredient
  13. {
  14. public string Name;
  15. public int Capacity;
  16. public int Durability;
  17. public int Flavor;
  18. public int Texture;
  19. public int Calories;
  20. }
  21. class Program
  22. {
  23. static int EvaluateRecipe((Recipe ingredient, int qt) recipe)
  24. {
  25. int capacity = 0, durability = 0, flavor = 0, texture = 0, calories = 0;
  26. while (recipe.ingredient != null)
  27. {
  28. (Recipe nrecipe, int qt) = recipe;
  29. var i = nrecipe.I;
  30. capacity += i.Capacity * qt;
  31. durability += i.Durability * qt;
  32. flavor += i.Flavor * qt;
  33. texture += i.Texture * qt;
  34. calories += i.Calories * qt;
  35. recipe = nrecipe.R;
  36. }
  37. if (calories != 500) return 0;
  38. return Math.Max(capacity, 0) * Math.Max(durability, 0) * Math.Max(flavor, 0) * Math.Max(texture, 0);
  39. }
  40. static int TestRecipes((Recipe ingredient, int qt) recipe, List<Ingredient> ingredients, int i = 0, int total = 0)
  41. {
  42. var ingr = ingredients[i];
  43. if (i == ingredients.Count - 1)
  44. {
  45. recipe = (new Recipe { R = recipe, I = ingr }, 100 - total);
  46. return EvaluateRecipe(recipe);
  47. }
  48. int bestRecipe = 0;
  49. for (int qt = 0; qt <= 100 - total; ++qt)
  50. {
  51. var result = TestRecipes((new Recipe { R = recipe, I = ingr }, qt), ingredients, i + 1, total + qt);
  52. if (result > bestRecipe) bestRecipe = result;
  53. }
  54. return bestRecipe;
  55. }
  56. static void Main(string[] args)
  57. {
  58. if (args.Length < 1) throw new ArgumentException();
  59. if (File.Exists(args[0]) == false) throw new FileNotFoundException();
  60. var ingredients = new List<Ingredient>();
  61. ParseFile(args[0], ingredients);
  62. int bestRecipe = TestRecipes((null, 0), ingredients);
  63. Console.WriteLine($"The answer is {bestRecipe}");
  64. }
  65. private static void ParseFile(string f, List<Ingredient> ingredients)
  66. {
  67. var regex = new Regex(@"(?<n>\w+): capacity (?<c>-?\d+), durability (?<d>-?\d+), flavor (?<f>-?\d+), texture (?<t>-?\d+), calories (?<cs>-?\d+)");
  68. using (var file = File.OpenText(f))
  69. {
  70. while (true)
  71. {
  72. var line = file.ReadLine();
  73. if (line == null) break;
  74. var result = regex.Match(line);
  75. var i = new Ingredient();
  76. i.Name = result.Groups["n"].Value;
  77. i.Capacity = int.Parse(result.Groups["c"].Value);
  78. i.Durability = int.Parse(result.Groups["d"].Value);
  79. i.Flavor = int.Parse(result.Groups["f"].Value);
  80. i.Texture = int.Parse(result.Groups["t"].Value);
  81. i.Calories = int.Parse(result.Groups["cs"].Value);
  82. ingredients.Add(i);
  83. }
  84. }
  85. }
  86. }
  87. }