bastien.monsarrat 6 年之前
父节点
当前提交
66c9851569
共有 7 个文件被更改,包括 177 次插入0 次删除
  1. 12 0
      Adv2016.sln
  2. 10 0
      D17.1/D17.1.csproj
  3. 62 0
      D17.1/Program.cs
  4. 8 0
      D17.1/Properties/launchSettings.json
  5. 10 0
      D17.2/D17.2.csproj
  6. 67 0
      D17.2/Program.cs
  7. 8 0
      D17.2/Properties/launchSettings.json

+ 12 - 0
Adv2016.sln

@@ -57,6 +57,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D15", "D15.1\D15.csproj", "
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D16", "D16.1\D16.csproj", "{4B98BF9C-FDAF-474B-BF8E-FF5F91983E2E}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D17.1", "D17.1\D17.1.csproj", "{6E257F44-5AAB-4C40-A208-8906D1ADDC6F}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D17.2", "D17.2\D17.2.csproj", "{428544E0-4EB6-42FE-B6F2-8334726F34E5}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -171,6 +175,14 @@ Global
 		{4B98BF9C-FDAF-474B-BF8E-FF5F91983E2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{4B98BF9C-FDAF-474B-BF8E-FF5F91983E2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{4B98BF9C-FDAF-474B-BF8E-FF5F91983E2E}.Release|Any CPU.Build.0 = Release|Any CPU
+		{6E257F44-5AAB-4C40-A208-8906D1ADDC6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{6E257F44-5AAB-4C40-A208-8906D1ADDC6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{6E257F44-5AAB-4C40-A208-8906D1ADDC6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{6E257F44-5AAB-4C40-A208-8906D1ADDC6F}.Release|Any CPU.Build.0 = Release|Any CPU
+		{428544E0-4EB6-42FE-B6F2-8334726F34E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{428544E0-4EB6-42FE-B6F2-8334726F34E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{428544E0-4EB6-42FE-B6F2-8334726F34E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{428544E0-4EB6-42FE-B6F2-8334726F34E5}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 10 - 0
D17.1/D17.1.csproj

@@ -0,0 +1,10 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D17._1</RootNamespace>
+    <LangVersion>7.3</LangVersion>
+  </PropertyGroup>
+
+</Project>

+ 62 - 0
D17.1/Program.cs

@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace D17._1
+{
+    class Program
+    {
+        static byte[] ToB(string str) => Encoding.ASCII.GetBytes(str);
+        static string ToS(byte[] b) => BitConverter.ToString(b).Replace("-", "");
+
+        private static string GetHash(string input, string sequence, MD5 md5) => ToS(md5.ComputeHash(ToB($"{input}{sequence}")));
+
+        static string SolveMaze((int x, int y) coord, (int x, int y) goal, string input)
+        {
+            var queue = new Queue<((int x, int y) coord, string sequence)>();
+            string sequence = null;
+            queue.Enqueue((coord, ""));
+
+            var md5 = MD5.Create();
+
+            while (queue.Count > 0)
+            {
+                var element = queue.Dequeue();
+                coord = element.coord;
+                sequence = element.sequence;
+
+                if (coord == (3, 3))
+                    break;
+
+                var hash = GetHash(input, sequence, md5);
+                for (int i = 0; i < 4; i++)
+                {
+                    if (hash[i] <= 'A') continue;
+
+                    (int mx, int my) = MoveSequence[i];
+                    (int x, int y) ncoord = (coord.x + mx, coord.y + my);
+
+                    if (ncoord.x < 0 || ncoord.y < 0 || ncoord.x > 3 || ncoord.y > 3)
+                        continue;
+
+                    var nsequence = sequence + DoorSequence[i];
+
+                    queue.Enqueue((ncoord, nsequence));
+                }
+            }
+
+            md5.Dispose();
+            return sequence;
+        }
+
+        static readonly char[] DoorSequence = new[] { 'U', 'D', 'L', 'R' };
+        static readonly (int mx, int my)[] MoveSequence = new[] { (0, -1), (0, 1), (-1, 0), (1, 0) };
+        static void Main(string[] args)
+        {
+            var seq = SolveMaze((0, 0), (3, 3), args[0]);
+
+            Console.WriteLine($"Sequence is : {seq}");
+        }
+    }
+}

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

@@ -0,0 +1,8 @@
+{
+  "profiles": {
+    "D17.1": {
+      "commandName": "Project",
+      "commandLineArgs": "awrkjxxr"
+    }
+  }
+}

+ 10 - 0
D17.2/D17.2.csproj

@@ -0,0 +1,10 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D17._2</RootNamespace>
+    <LangVersion>7.3</LangVersion>
+  </PropertyGroup>
+
+</Project>

+ 67 - 0
D17.2/Program.cs

@@ -0,0 +1,67 @@
+using System;
+using System.Collections.Generic;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace D17._2
+{
+    class Program
+    {
+        static byte[] ToB(string str) => Encoding.ASCII.GetBytes(str);
+        static string ToS(byte[] b) => BitConverter.ToString(b).Replace("-", "");
+
+        private static string GetHash(string input, string sequence, MD5 md5) => ToS(md5.ComputeHash(ToB($"{input}{sequence}")));
+
+        static string SolveMaze((int x, int y) coord, (int x, int y) goal, string input)
+        {
+            var queue = new Queue<((int x, int y) coord, string sequence)>();
+            string sequence = null;
+            queue.Enqueue((coord, ""));
+
+            var longest = "";
+
+            var md5 = MD5.Create();
+
+            while (queue.Count > 0)
+            {
+                var element = queue.Dequeue();
+                coord = element.coord;
+                sequence = element.sequence;
+
+                if (coord == (3, 3))
+                {
+                    if (sequence.Length > longest.Length) longest = sequence;
+                    continue;
+                }
+
+                var hash = GetHash(input, sequence, md5);
+                for (int i = 0; i < 4; i++)
+                {
+                    if (hash[i] <= 'A') continue;
+
+                    (int mx, int my) = MoveSequence[i];
+                    (int x, int y) ncoord = (coord.x + mx, coord.y + my);
+
+                    if (ncoord.x < 0 || ncoord.y < 0 || ncoord.x > 3 || ncoord.y > 3)
+                        continue;
+
+                    var nsequence = sequence + DoorSequence[i];
+
+                    queue.Enqueue((ncoord, nsequence));
+                }
+            }
+
+            md5.Dispose();
+            return longest;
+        }
+
+        static readonly char[] DoorSequence = new[] { 'U', 'D', 'L', 'R' };
+        static readonly (int mx, int my)[] MoveSequence = new[] { (0, -1), (0, 1), (-1, 0), (1, 0) };
+        static void Main(string[] args)
+        {
+            var seq = SolveMaze((0, 0), (3, 3), args[0]);
+
+            Console.WriteLine($"Longest sequence size is : {seq.Length}");
+        }
+    }
+}

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

@@ -0,0 +1,8 @@
+{
+  "profiles": {
+    "D17.2": {
+      "commandName": "Project",
+      "commandLineArgs": "awrkjxxr"
+    }
+  }
+}