bastien.monsarrat 6 жил өмнө
parent
commit
142f0726cd

+ 13 - 1
Adv2015.sln

@@ -67,7 +67,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D17", "D17.1\D17.csproj", "
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D18.1", "D18.1\D18.1.csproj", "{90F84068-1388-4124-8ED5-DB658383D961}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D18.2", "D18.2\D18.2.csproj", "{BF47427D-176C-4059-9DEC-EA709E7F28A0}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D18.2", "D18.2\D18.2.csproj", "{BF47427D-176C-4059-9DEC-EA709E7F28A0}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D19.1", "D19.1\D19.1.csproj", "{8FDDC40C-F103-4B7D-AF53-A5BAC95B3566}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D19.2", "D19.2\D19.2.csproj", "{BE97EB79-038F-445E-9604-C77CA4CEE115}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -207,6 +211,14 @@ Global
 		{BF47427D-176C-4059-9DEC-EA709E7F28A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{BF47427D-176C-4059-9DEC-EA709E7F28A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{BF47427D-176C-4059-9DEC-EA709E7F28A0}.Release|Any CPU.Build.0 = Release|Any CPU
+		{8FDDC40C-F103-4B7D-AF53-A5BAC95B3566}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{8FDDC40C-F103-4B7D-AF53-A5BAC95B3566}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{8FDDC40C-F103-4B7D-AF53-A5BAC95B3566}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{8FDDC40C-F103-4B7D-AF53-A5BAC95B3566}.Release|Any CPU.Build.0 = Release|Any CPU
+		{BE97EB79-038F-445E-9604-C77CA4CEE115}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{BE97EB79-038F-445E-9604-C77CA4CEE115}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{BE97EB79-038F-445E-9604-C77CA4CEE115}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{BE97EB79-038F-445E-9604-C77CA4CEE115}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

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

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

+ 65 - 0
D19.1/Program.cs

@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace D19._1
+{
+    public class Program
+    {
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) throw new ArgumentException();
+            if (File.Exists(args[0]) == false) throw new FileNotFoundException();
+
+            string input;
+            List<(string, string)> replacements;
+            ParseFile(args, out input, out replacements);
+
+            var molecules = TestMolecules(input, replacements);
+
+            Console.WriteLine($"The answer is : {molecules.Count}");
+        }
+
+        public static void ParseFile(string[] args, out string input, out List<(string, string)> replacements)
+        {
+            input = string.Empty;
+            replacements = new List<(string, string)>();
+            using (var file = File.OpenText(args[0]))
+            {
+                while (true)
+                {
+                    var line = file.ReadLine();
+                    if (line == null) break;
+
+                    if (line == string.Empty) continue;
+
+                    var lr = line.Split(" => ");
+
+                    if (lr.Length == 2) replacements.Add((lr[0], lr[1]));
+                    else input = lr[0];
+                }
+            }
+        }
+
+        public static HashSet<string> TestMolecules(string input, List<(string, string)> replacements)
+        {
+            HashSet<string> molecules = new HashSet<string>();
+            foreach ((string orig, string replace) in replacements)
+            {
+                int index = 0;
+
+                while (true)
+                {
+                    index = input.IndexOf(orig, index);
+                    if (index == -1) break;
+
+                    string molecule = input.Substring(0, index) + replace + input.Substring(index + orig.Length);
+                    molecules.Add(molecule);
+
+                    index++;
+                }
+            }
+            return molecules;
+        }
+    }
+}

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

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

+ 45 - 0
D19.1/input.txt

@@ -0,0 +1,45 @@
+Al => ThF
+Al => ThRnFAr
+B => BCa
+B => TiB
+B => TiRnFAr
+Ca => CaCa
+Ca => PB
+Ca => PRnFAr
+Ca => SiRnFYFAr
+Ca => SiRnMgAr
+Ca => SiTh
+F => CaF
+F => PMg
+F => SiAl
+H => CRnAlAr
+H => CRnFYFYFAr
+H => CRnFYMgAr
+H => CRnMgYFAr
+H => HCa
+H => NRnFYFAr
+H => NRnMgAr
+H => NTh
+H => OB
+H => ORnFAr
+Mg => BF
+Mg => TiMg
+N => CRnFAr
+N => HSi
+O => CRnFYFAr
+O => CRnMgAr
+O => HP
+O => NRnFAr
+O => OTi
+P => CaP
+P => PTi
+P => SiRnFAr
+Si => CaSi
+Th => ThCa
+Ti => BP
+Ti => TiTi
+e => HF
+e => NAl
+e => OMg
+
+CRnCaCaCaSiRnBPTiMgArSiRnSiRnMgArSiRnCaFArTiTiBSiThFYCaFArCaCaSiThCaPBSiThSiThCaCaPTiRnPBSiThRnFArArCaCaSiThCaSiThSiRnMgArCaPTiBPRnFArSiThCaSiRnFArBCaSiRnCaPRnFArPMgYCaFArCaPTiTiTiBPBSiThCaPTiBPBSiRnFArBPBSiRnCaFArBPRnSiRnFArRnSiRnBFArCaFArCaCaCaSiThSiThCaCaPBPTiTiRnFArCaPTiBSiAlArPBCaCaCaCaCaSiRnMgArCaSiThFArThCaSiThCaSiRnCaFYCaSiRnFYFArFArCaSiRnFYFArCaSiRnBPMgArSiThPRnFArCaSiRnFArTiRnSiRnFYFArCaSiRnBFArCaSiRnTiMgArSiThCaSiThCaFArPRnFArSiRnFArTiTiTiTiBCaCaSiRnCaCaFYFArSiThCaPTiBPTiBCaSiThSiRnMgArCaF

+ 13 - 0
D19.2/D19.2.csproj

@@ -0,0 +1,13 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D19._2</RootNamespace>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\D19.1\D19.1.csproj" />
+  </ItemGroup>
+
+</Project>

+ 74 - 0
D19.2/Program.cs

@@ -0,0 +1,74 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace D19._2
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) throw new ArgumentException();
+            if (File.Exists(args[0]) == false) throw new FileNotFoundException();
+
+            string input;
+            List<(string, string)> replacements;
+            _1.Program.ParseFile(args, out input, out replacements);
+
+            replacements = replacements.Select(r => (r.Item2, r.Item1)).ToList();
+
+            var steps = TestMolecules(input, "e", replacements);
+
+            Console.WriteLine($"The answer is : {steps}");
+        }
+
+        private static void ParseFile(string[] args, out string input, out List<(string, string)> replacements)
+        {
+            input = string.Empty;
+            replacements = new List<(string, string)>();
+            using (var file = File.OpenText(args[0]))
+            {
+                while (true)
+                {
+                    var line = file.ReadLine();
+                    if (line == null) break;
+
+                    if (line == string.Empty) continue;
+
+                    var lr = line.Split(" => ");
+
+                    if (lr.Length == 2) replacements.Add((lr[0], lr[1]));
+                    else input = lr[0];
+                }
+            }
+        }
+
+        private static int TestMolecules(string input, string find, List<(string, string)> replacements)
+        {
+            var tested = new HashSet<string>();
+            var buildQueue = new List<(string molecule, int step)>();
+
+            buildQueue.Add((input, 0));
+
+            while (buildQueue.Count > 0)
+            {
+                (var mol, int step) = buildQueue.ElementAt(0);
+                buildQueue.RemoveAt(0);
+
+                input = mol;
+                tested.Add(input);
+
+                if (input == find) return step;
+
+                foreach (var molecule in _1.Program.TestMolecules(input, replacements).OrderByDescending(s => s.Length))
+                {
+                    if (tested.Contains(molecule) == false)
+                        buildQueue.Insert(0, (molecule, step + 1));
+                }
+            }
+
+            return 0;
+        }
+    }
+}

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

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