bastien.monsarrat 6 years ago
parent
commit
77c4b5cabc

+ 12 - 0
Adv2015.sln

@@ -55,6 +55,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D14.1", "D14.1\D14.1.csproj
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D14.2", "D14.2\D14.2.csproj", "{D55FCBDF-60C8-4DCE-B788-4799FE19CAB3}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D15.1", "D15.1\D15.1.csproj", "{03B01AF1-879A-46D4-BDD6-12F1443476FE}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D15.2", "D15.2\D15.2.csproj", "{7AED7764-630F-44E6-850C-23093BA54E7F}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -165,6 +169,14 @@ Global
 		{D55FCBDF-60C8-4DCE-B788-4799FE19CAB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{D55FCBDF-60C8-4DCE-B788-4799FE19CAB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{D55FCBDF-60C8-4DCE-B788-4799FE19CAB3}.Release|Any CPU.Build.0 = Release|Any CPU
+		{03B01AF1-879A-46D4-BDD6-12F1443476FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{03B01AF1-879A-46D4-BDD6-12F1443476FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{03B01AF1-879A-46D4-BDD6-12F1443476FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{03B01AF1-879A-46D4-BDD6-12F1443476FE}.Release|Any CPU.Build.0 = Release|Any CPU
+		{7AED7764-630F-44E6-850C-23093BA54E7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{7AED7764-630F-44E6-850C-23093BA54E7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{7AED7764-630F-44E6-850C-23093BA54E7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{7AED7764-630F-44E6-850C-23093BA54E7F}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 9 - 0
D15.1/D15.1.csproj

@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D15._1</RootNamespace>
+  </PropertyGroup>
+
+</Project>

+ 104 - 0
D15.1/Program.cs

@@ -0,0 +1,104 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text.RegularExpressions;
+
+namespace D15._1
+{
+    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;
+
+            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;
+
+                recipe = nrecipe.R;
+            }
+
+            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);
+                }
+            }
+        }
+    }
+}

+ 8 - 0
D15.1/Properties/launchSettings.json

@@ -0,0 +1,8 @@
+{
+  "profiles": {
+    "D15.1": {
+      "commandName": "Project",
+      "commandLineArgs": "\"..\\..\\..\\..\\D15.1\\input.txt\""
+    }
+  }
+}

+ 4 - 0
D15.1/input.txt

@@ -0,0 +1,4 @@
+Sprinkles: capacity 2, durability 0, flavor -2, texture 0, calories 3
+Butterscotch: capacity 0, durability 5, flavor -3, texture 0, calories 3
+Chocolate: capacity 0, durability 0, flavor 5, texture -1, calories 8
+Candy: capacity 0, durability -1, flavor 0, texture 5, calories 8

+ 9 - 0
D15.2/D15.2.csproj

@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D15._2</RootNamespace>
+  </PropertyGroup>
+
+</Project>

+ 106 - 0
D15.2/Program.cs

@@ -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);
+                }
+            }
+        }
+    }
+}

+ 8 - 0
D15.2/Properties/launchSettings.json

@@ -0,0 +1,8 @@
+{
+  "profiles": {
+    "D15.2": {
+      "commandName": "Project",
+      "commandLineArgs": "\"..\\..\\..\\..\\D15.1\\input.txt\""
+    }
+  }
+}