bastien.monsarrat 6 years ago
parent
commit
4c166bcc81

+ 12 - 0
Adv2016.sln

@@ -47,6 +47,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D12.1", "D12.1\D12.1.csproj
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D12.2", "D12.2\D12.2.csproj", "{873596CF-84EA-4318-A43F-909284891A82}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D13.1", "D13.1\D13.1.csproj", "{D7808973-C957-41B8-81B4-875F207C16FA}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D13.2", "D13.2\D13.2.csproj", "{6747B6EB-EBFA-4E9A-B390-DEB2DE8B84F9}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -141,6 +145,14 @@ Global
 		{873596CF-84EA-4318-A43F-909284891A82}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{873596CF-84EA-4318-A43F-909284891A82}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{873596CF-84EA-4318-A43F-909284891A82}.Release|Any CPU.Build.0 = Release|Any CPU
+		{D7808973-C957-41B8-81B4-875F207C16FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{D7808973-C957-41B8-81B4-875F207C16FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{D7808973-C957-41B8-81B4-875F207C16FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{D7808973-C957-41B8-81B4-875F207C16FA}.Release|Any CPU.Build.0 = Release|Any CPU
+		{6747B6EB-EBFA-4E9A-B390-DEB2DE8B84F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{6747B6EB-EBFA-4E9A-B390-DEB2DE8B84F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{6747B6EB-EBFA-4E9A-B390-DEB2DE8B84F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{6747B6EB-EBFA-4E9A-B390-DEB2DE8B84F9}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

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

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

+ 61 - 0
D13.1/Program.cs

@@ -0,0 +1,61 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+
+namespace D13._1
+{
+    class Program
+    {
+        static bool IsOpenSpace((int x, int y) coord, int fav)
+        {
+            var n = (coord.x * coord.x) + (3 * coord.x) + (2 * coord.x * coord.y) + coord.y + (coord.y * coord.y) + fav;
+            int nbits = 0;
+            while (n > 0)
+            {
+                nbits += n & 1;
+                n >>= 1;
+            }
+            return nbits % 2 == 0;
+        }
+
+        static readonly (int mx, int my)[] Moves = new [] { (-1, 0), (0, -1), (1, 0), (0, 1) };
+
+        static int SolveMaze((int x, int y) start, (int x, int y) goal, int fav)
+        {
+            var queue = new Queue<((int x, int y), int step)>();
+            var visited = new HashSet<(int, int)>();
+
+            queue.Enqueue((start, 0));
+
+            while (queue.Count > 0)
+            {
+                ((int x, int y) coord, int step) = queue.Dequeue();
+                visited.Add(coord);
+
+                if (coord == goal) return step;
+
+                foreach (var (mx, my) in Moves)
+                {
+                    (int x, int y) ncoord = (coord.x + mx, coord.y + my);
+                    if (ncoord.x < 0 || ncoord.y < 0) continue;
+
+                    if (IsOpenSpace(ncoord, fav) == false) continue;
+                    if (visited.Contains(ncoord) == true) continue;
+                    queue.Enqueue((ncoord, step + 1));
+                }
+            }
+            throw new Exception();
+        }
+
+        static void Main(string[] args)
+        {
+            var sw = Stopwatch.StartNew();
+
+            var answer = SolveMaze((1, 1), (31, 39), int.Parse(args[0]));
+            Console.WriteLine($"The answer is : {answer}\n");
+
+            sw.Stop();
+            Console.WriteLine($"Execution time : {sw.ElapsedMilliseconds}ms");
+        }
+    }
+}

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

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

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

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

+ 65 - 0
D13.2/Program.cs

@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+
+namespace D13._1
+{
+    class Program
+    {
+        static bool IsOpenSpace((int x, int y) coord, int fav)
+        {
+            var n = (coord.x * coord.x) + (3 * coord.x) + (2 * coord.x * coord.y) + coord.y + (coord.y * coord.y) + fav;
+            int nbits = 0;
+            while (n > 0)
+            {
+                nbits += n & 1;
+                n >>= 1;
+            }
+            return nbits % 2 == 0;
+        }
+
+        static readonly (int mx, int my)[] Moves = new[] { (-1, 0), (0, -1), (1, 0), (0, 1) };
+
+        static int SolveMaze((int x, int y) start, int goal, int fav)
+        {
+            int answer = 0;
+            var queue = new Queue<((int x, int y), int step)>();
+            var visited = new HashSet<(int, int)>();
+
+            queue.Enqueue((start, 0));
+
+            while (queue.Count > 0)
+            {
+                ((int x, int y) coord, int step) = queue.Dequeue();
+
+                if (step == goal) continue;
+                answer++;
+
+                foreach (var (mx, my) in Moves)
+                {
+                    (int x, int y) ncoord = (coord.x + mx, coord.y + my);
+                    if (ncoord.x < 0 || ncoord.y < 0) continue;
+
+                    if (IsOpenSpace(ncoord, fav) == false) continue;
+                    if (visited.Contains(ncoord) == true) continue;
+
+                    visited.Add(ncoord);
+                    queue.Enqueue((ncoord, step + 1));
+                }
+            }
+
+            return answer;
+        }
+
+        static void Main(string[] args)
+        {
+            var sw = Stopwatch.StartNew();
+
+            var answer = SolveMaze((1, 1), 50, int.Parse(args[0]));
+            Console.WriteLine($"The answer is : {answer}\n");
+
+            sw.Stop();
+            Console.WriteLine($"Execution time : {sw.ElapsedMilliseconds}ms");
+        }
+    }
+}

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

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