Program.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. namespace D15._1
  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;
  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. recipe = nrecipe.R;
  35. }
  36. return Math.Max(capacity, 0) * Math.Max(durability, 0) * Math.Max(flavor, 0) * Math.Max(texture, 0);
  37. }
  38. static int TestRecipes((Recipe ingredient, int qt) recipe, List<Ingredient> ingredients, int i = 0, int total = 0)
  39. {
  40. var ingr = ingredients[i];
  41. if (i == ingredients.Count - 1)
  42. {
  43. recipe = (new Recipe { R = recipe, I = ingr }, 100 - total);
  44. return EvaluateRecipe(recipe);
  45. }
  46. int bestRecipe = 0;
  47. for (int qt = 0; qt <= 100 - total; ++qt)
  48. {
  49. var result = TestRecipes((new Recipe { R = recipe, I = ingr }, qt), ingredients, i + 1, total + qt);
  50. if (result > bestRecipe) bestRecipe = result;
  51. }
  52. return bestRecipe;
  53. }
  54. static void Main(string[] args)
  55. {
  56. if (args.Length < 1) throw new ArgumentException();
  57. if (File.Exists(args[0]) == false) throw new FileNotFoundException();
  58. var ingredients = new List<Ingredient>();
  59. ParseFile(args[0], ingredients);
  60. int bestRecipe = TestRecipes((null, 0), ingredients);
  61. Console.WriteLine($"The answer is {bestRecipe}");
  62. }
  63. private static void ParseFile(string f, List<Ingredient> ingredients)
  64. {
  65. var regex = new Regex(@"(?<n>\w+): capacity (?<c>-?\d+), durability (?<d>-?\d+), flavor (?<f>-?\d+), texture (?<t>-?\d+), calories (?<cs>-?\d+)");
  66. using (var file = File.OpenText(f))
  67. {
  68. while (true)
  69. {
  70. var line = file.ReadLine();
  71. if (line == null) break;
  72. var result = regex.Match(line);
  73. var i = new Ingredient();
  74. i.Name = result.Groups["n"].Value;
  75. i.Capacity = int.Parse(result.Groups["c"].Value);
  76. i.Durability = int.Parse(result.Groups["d"].Value);
  77. i.Flavor = int.Parse(result.Groups["f"].Value);
  78. i.Texture = int.Parse(result.Groups["t"].Value);
  79. i.Calories = int.Parse(result.Groups["cs"].Value);
  80. ingredients.Add(i);
  81. }
  82. }
  83. }
  84. }
  85. }