|
|
@@ -0,0 +1,106 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
+
|
|
|
+namespace D15._2
|
|
|
+{
|
|
|
+ public class Recipe
|
|
|
+ {
|
|
|
+ public Ingredient I;
|
|
|
+ public (Recipe R, int Qt) R;
|
|
|
+ }
|
|
|
+
|
|
|
+ public class Ingredient
|
|
|
+ {
|
|
|
+ public string Name;
|
|
|
+ public int Capacity;
|
|
|
+ public int Durability;
|
|
|
+ public int Flavor;
|
|
|
+ public int Texture;
|
|
|
+ public int Calories;
|
|
|
+ }
|
|
|
+
|
|
|
+ class Program
|
|
|
+ {
|
|
|
+ static int EvaluateRecipe((Recipe ingredient, int qt) recipe)
|
|
|
+ {
|
|
|
+ int capacity = 0, durability = 0, flavor = 0, texture = 0, calories = 0;
|
|
|
+
|
|
|
+ while (recipe.ingredient != null)
|
|
|
+ {
|
|
|
+ (Recipe nrecipe, int qt) = recipe;
|
|
|
+ var i = nrecipe.I;
|
|
|
+
|
|
|
+ capacity += i.Capacity * qt;
|
|
|
+ durability += i.Durability * qt;
|
|
|
+ flavor += i.Flavor * qt;
|
|
|
+ texture += i.Texture * qt;
|
|
|
+ calories += i.Calories * qt;
|
|
|
+
|
|
|
+ recipe = nrecipe.R;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (calories != 500) return 0;
|
|
|
+ return Math.Max(capacity, 0) * Math.Max(durability, 0) * Math.Max(flavor, 0) * Math.Max(texture, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ static int TestRecipes((Recipe ingredient, int qt) recipe, List<Ingredient> ingredients, int i = 0, int total = 0)
|
|
|
+ {
|
|
|
+ var ingr = ingredients[i];
|
|
|
+ if (i == ingredients.Count - 1)
|
|
|
+ {
|
|
|
+ recipe = (new Recipe { R = recipe, I = ingr }, 100 - total);
|
|
|
+ return EvaluateRecipe(recipe);
|
|
|
+ }
|
|
|
+
|
|
|
+ int bestRecipe = 0;
|
|
|
+ for (int qt = 0; qt <= 100 - total; ++qt)
|
|
|
+ {
|
|
|
+ var result = TestRecipes((new Recipe { R = recipe, I = ingr }, qt), ingredients, i + 1, total + qt);
|
|
|
+ if (result > bestRecipe) bestRecipe = result;
|
|
|
+ }
|
|
|
+
|
|
|
+ return bestRecipe;
|
|
|
+ }
|
|
|
+
|
|
|
+ static void Main(string[] args)
|
|
|
+ {
|
|
|
+ if (args.Length < 1) throw new ArgumentException();
|
|
|
+ if (File.Exists(args[0]) == false) throw new FileNotFoundException();
|
|
|
+
|
|
|
+ var ingredients = new List<Ingredient>();
|
|
|
+
|
|
|
+ ParseFile(args[0], ingredients);
|
|
|
+
|
|
|
+ int bestRecipe = TestRecipes((null, 0), ingredients);
|
|
|
+
|
|
|
+ Console.WriteLine($"The answer is {bestRecipe}");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void ParseFile(string f, List<Ingredient> ingredients)
|
|
|
+ {
|
|
|
+ var regex = new Regex(@"(?<n>\w+): capacity (?<c>-?\d+), durability (?<d>-?\d+), flavor (?<f>-?\d+), texture (?<t>-?\d+), calories (?<cs>-?\d+)");
|
|
|
+ using (var file = File.OpenText(f))
|
|
|
+ {
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ var line = file.ReadLine();
|
|
|
+ if (line == null) break;
|
|
|
+ var result = regex.Match(line);
|
|
|
+
|
|
|
+ var i = new Ingredient();
|
|
|
+
|
|
|
+ i.Name = result.Groups["n"].Value;
|
|
|
+ i.Capacity = int.Parse(result.Groups["c"].Value);
|
|
|
+ i.Durability = int.Parse(result.Groups["d"].Value);
|
|
|
+ i.Flavor = int.Parse(result.Groups["f"].Value);
|
|
|
+ i.Texture = int.Parse(result.Groups["t"].Value);
|
|
|
+ i.Calories = int.Parse(result.Groups["cs"].Value);
|
|
|
+
|
|
|
+ ingredients.Add(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|