bastien.monsarrat 6 năm trước cách đây
mục cha
commit
7dc450c4de

+ 12 - 0
Adv2016.sln

@@ -81,6 +81,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D23.1", "D23.1\D23.1.csproj
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D23.2", "D23.2\D23.2.csproj", "{FAA8466D-15B7-46FC-A6CD-ECE8EDC8EBD1}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D24.1", "D24.1\D24.1.csproj", "{6B8B4A42-A91E-4348-A4D9-581F64774317}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D24.2", "D24.2\D24.2.csproj", "{35B60F35-4C11-437E-8B4D-8EE5F0E717B1}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -243,6 +247,14 @@ Global
 		{FAA8466D-15B7-46FC-A6CD-ECE8EDC8EBD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{FAA8466D-15B7-46FC-A6CD-ECE8EDC8EBD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{FAA8466D-15B7-46FC-A6CD-ECE8EDC8EBD1}.Release|Any CPU.Build.0 = Release|Any CPU
+		{6B8B4A42-A91E-4348-A4D9-581F64774317}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{6B8B4A42-A91E-4348-A4D9-581F64774317}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{6B8B4A42-A91E-4348-A4D9-581F64774317}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{6B8B4A42-A91E-4348-A4D9-581F64774317}.Release|Any CPU.Build.0 = Release|Any CPU
+		{35B60F35-4C11-437E-8B4D-8EE5F0E717B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{35B60F35-4C11-437E-8B4D-8EE5F0E717B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{35B60F35-4C11-437E-8B4D-8EE5F0E717B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{35B60F35-4C11-437E-8B4D-8EE5F0E717B1}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

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

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

+ 96 - 0
D24.1/Program.cs

@@ -0,0 +1,96 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace D24._1
+{
+    public class Program
+    {
+        public static int Solve(bool[][] map, (int, int) start, (int, int) goal)
+        {
+            var queue = new Queue<((int x, int y) coord, int steps)>();
+            HashSet<(int, int)> visited = new HashSet<(int, int)>();
+
+            queue.Enqueue((start, 0));
+
+            while (queue.Count > 0)
+            {
+                var (coord, steps) = queue.Dequeue();
+
+                if (coord == goal)
+                    return steps;
+
+                foreach ((int mx, int my) in Move)
+                {
+                    (int x, int y) ncoord = (coord.x + mx, coord.y + my);
+                    if (map[ncoord.y][ncoord.x]) continue;
+
+                    if (visited.Contains(ncoord)) continue;
+                    visited.Add(ncoord);
+
+                    queue.Enqueue((ncoord, steps + 1));
+                }
+            }
+
+            throw new Exception();
+        }
+
+        static readonly (int mx, int my)[] Move = new[] { (-1, 0), (0, -1), (1, 0), (0, 1) };
+
+        static void Main(string[] args)
+        {
+
+            var file = File.ReadAllText(args[0]).Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
+
+            var poi = new Dictionary<int, (int x, int y)>();
+
+            var map = ParseMap(file, poi);
+
+            var perm = GetPermutations(poi.Keys.OrderBy(p => p).Skip(1), poi.Count - 1);
+
+            int minStep = int.MaxValue;
+            foreach (var p in perm)
+            {
+                var current = 0;
+                int step = 0;
+                foreach (var destination in p)
+                {
+                    step += Solve(map, poi[current], poi[destination]);
+                    current = destination;
+                }
+
+                if (step < minStep) minStep = step;
+            }
+
+            Console.WriteLine($"The answer is : {minStep}");
+        }
+
+        public static bool[][] ParseMap(string[] file, Dictionary<int, (int x, int y)> poi)
+        {
+            bool[][] map = new bool[file.Length][];
+
+            int l = 0;
+            foreach (string line in file)
+            {
+                map[l] = new bool[line.Length];
+                for (var i = 0; i < line.Length; ++i)
+                {
+                    if (line[i] >= '0' && line[i] <= '9') poi.Add(line[i] - '0', (i, l));
+                    map[l][i] = line[i] == '#';
+                }
+                l++;
+            }
+
+            return map;
+        }
+
+        public static IEnumerable<IEnumerable<int>> GetPermutations(IEnumerable<int> list, int length)
+        {
+            if (length == 1) return list.Select(t => new [] { t });
+
+            return GetPermutations(list, length - 1).SelectMany(t => list.Where(e => !t.Contains(e)),
+                    (t1, t2) => t1.Concat(new [] { t2 }));
+        }
+    }
+}

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

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

+ 43 - 0
D24.1/input.txt

@@ -0,0 +1,43 @@
+#####################################################################################################################################################################################
+#.....#...#.#...............#...........#.......#.#...#.......#...#.......#.....#.............#.........#...#.........#.......#.#.#4....#.....#...#.......#.........#.#.....#.......#
+###.###.#.#.###.###.#.#####.#.###.#.###.#.#.#.#.#.#.#.###.###.#.#.#.###.#.#.###.#.###.#.#.#######.#.#.#.###.#.#.#.###.#.#####.#.#.###.###.#.###.#.###.#.#.#.###.###.#.#.#.#.#.#.#.###
+#...#.....#.....#...#.......#.#.#.#.#...#.....#.#...#.....#...#...#.#...#.#.......#.....#.#.........#...#.#.#...#...#.........#...#.......#.#...#.........#.#...#...#.#.#.#.....#...#
+#.###.###.#.#.#.#.#######.#.#.#.#.#.###.#.#.###.#.###.#.###.#.#.#.#########.###.#.#.#.###.###.#.###.###.#.#.#.#.#####.#.###.#.#.###.#.#.#.#.###.###.#.#####.#.#####.#.###.#.###.#.#.#
+#.#.....#.#.....#.....#...#.....#.#...#...#.#.......#.#.#.....#.......#...#...#.#.#.#.....#...#.#.#.....#...#.#.#...........#...#...#.#.#.....#...........#.#.#.......#.............#
+#.#####.#####.#.#####.#.#.#.###.#.#.#.#.#.#####.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.###.#.#.#.###.#.#.#.###.#.#.#.###.#.#.###########.#.###.#.#.#.#.#.#.#.#####.#.###.#.#####.###.#.#
+#...#.....#.......#.#.#.#...#...#...#.#.#.......#.....#.........#.#...........#.#.........#.#.....................#.......#.....#.............#.........#.....#.....#...#.#.#.......#
+#.###.#.#.#.#.#.#.#.###.#.#.#.#.#.#.#.#####.#####.#.###.#.#.###.#.#####.###.#.#.#.#######.#.#.#######.#.#.###.#.#.#.#######.###.###.###.#######.#.#####.#.#.#.#.###.###.#.#.#######.#
+#...............#...#.#6..#...#...#...#...#.........#...#.#.....#.....#...#.#.....#.......#...#.#.....#...#.....#.........#...........#.#...........#...#0..#.............#.#...#...#
+#.#.#####.#########.#.#####.#.#.#######.#.#.#.#.###.###.#.###.#.#####.#.#.#.#########.#.#######.#.#.#.#.#.#.#.#.#.###.###.#.#.#######.###.#########.#.#.###.#.#.#.#.#.###.#.#.###.#.#
+#.#.....#.#.#...#.....#.....#...#.............#...#.......#...#...#...#.#.#.....#.#...#.......#.#...#.......#.#...#...#.......#.....#...#...#...#.#...#.....#.#...#...#.#...#.#...#.#
+#.###.#.###.#.#.#.###.###.#.###.#.#.###.#####.#####.#.#####.###.#.#######.###.#.#.#########.###.#.#.#.###.#.#.###.###.#.###.###.###.#.#.#####.###.#.#.###.#.#.###.#.###.#####.#.###.#
+#.#.........#.#.........#.......#...#.........#.................#.....#.#.......#.#.....#.#.....#.#.........#.....#.#.......#.#.............#.....#...#.....#.....#.......#.......#.#
+###.#####.#.#.#.#.#.###.#.#.#.#.#.#.#.#.#.###.#.#.#.#.###.###.#.#.#.###.#.#.###.#.#.#.###.#.#.#####.###.###.#.###.#.###.###.#.###.#.###.#####.#.#.#.#####.#.#.#.#####.###.#.#.###.#.#
+#...#...#.#...#.....#...#...#...#...#.#.......#.#.#.....#...#.#.....#.........#.#...#.......#.......#.#...#.....#...#.............#...............#.....#.#.....#...#.#...#.........#
+#.###.#.#.#.#.#.#.#.#.#####.#####.#.#####.#######.#####.#.#########.###.#.###.#.#.###.#.#.#.#.#.#.#.#.#.#.###.#.###.#.#.#.#.###.#.#.#.###.#####.###.#.#.#.#.#.#.#.#.#.#.#.#.#####.###
+#.#.....#...#.......#...#.#.#.#.#...#...#...#...#...................#...........#.....#.#...#.........#.#.......#.#...#.#.#.#...................#...#...#.#...#.....#.#1#.....#.....#
+###.#.#####.#.#.#.#.###.#.###.#.#.#.#.#.###.#.#.#.#.#.#.#####.###.###.#.#.#.#.#.#.###.#.#.#.###.#######.#.###.#.#.#.#.###.#.#########.#.#.#.#.#.###.#.#.#####.###.#.#########.#.#.#.#
+#.#...#.....#.....#...#.#.#...#.......#...#.....#.#...#.....#...#.....#...#.#.....#.......#...#...#.......#...#...#...#...#...........#.#.....#...#.........#.#.....#.#.#.......#...#
+#.#.#.#.#####.#.#.###.#.#.#.#.#.###.#.###.#.#.###.#.#.###.#.###.#.###.#####.#.#.#.#.#.#.###.#.#.#.#.#.###.#####.#.#.###.#.#.###.#.#.#.#.#.#.#.#####.#.#.###.#.#.###.#.#.###.#.#.###.#
+#.....#7#.#...#.#.................#...............#.#.#...#.#...#.#...#.....#...#.....#...#.#.#.......#...#...........#...........#.#.....#...#.........#.#.......#.#.#.....#.......#
+#.#.#.###.#.#.###.#.#.#.#.#.#.###.#.#.#.###.#.###.#.#.#.#.#.#.#.###.#.#.#.#####.#.#.#.###.#.#.#####.#.#.###########.#.#####.#.#.#.###.#.#.#.#.#####.#.###.###.#.###.#.#.###.#.#######
+#.#.....#.#.#.#...........#.#.#.......#...........#...#.......#...#.#...#.....#.#.....#...#.....#.#.......#.#.#.......#.#.......#.............#.#...#.#.#.#...........#.......#.....#
+#.#####.#####.#.#.###.#.#.#.#.#####.#.#.#.###.###.#.#.#.#.#######.#.#.#.#.###.#.#.#.#.#.#.#######.#.###.###.#.#.#.#.#.#.#.#.#####.#.#.#######.#.#.###.#.#.#####.###.###.#.#.#.#.#.#.#
+#.......#.#...#.....#...#.......#...#...#.....#...#.#...#...#.....#.#.#.#.....#.#.....#.....#...#...#...#.....#.#...#.#...#...#.............#.#...#.....#...#.....#.....#.#.....#.#.#
+#.#.#####.#.###.#########.###.#.#.#.###.#.#.###.#.#.#.#.#.#.#.###.#.###.###.###.#####.#.###.#.#.#.#.#.#.#.###.#####.#.#.#.#.#.###.#.#.#.###.#.###.###.#.#.#.#.###.#.###.###.###.#.###
+#...#.....#.#.#.#.........#...#.....#.........#...#.....#.....#.....#...................#.......#...#...#.....#.......#...#...#.......#.....#.#...#.#...#.#.........#.#.#...#.#.#...#
+###.#.#.#.#.#.#.#.###.#.#.#######.###.#######.#####.#.#.#.###.#.#.###.#######.###.#.###.###########.#.#####.###.#####.#.#######.#########.###.###.#.#.#.#.#.#.#.#.#.#.###.#.#.#.#.#.#
+#.....#...............#.............#...#.#.#.........#.#.....#...#.....#...#.....#.#.....#...#.#...#.#.....#.........#.#...............#.#.#...#.......#.#.....#.#...............#.#
+#.#.###.#.###.###.#.#.#.#.###.#.#.#.#.#.#.#.#####.#.###.#.#.#.#.#.#.#.###.#.###.#.#.#.#.#.###.#.#.#.###.###.#.#####.#.#.#.#.#.#.#######.#.#.###.###.#.#.#.#.#.#.#.#.#####.#####.#.###
+#...#...#...............#.......#...#.....#.....#.#...#.........#.#...#...#.....#...#.#...#...#...#...#.....#.#.....#.....#.#...#.....#.........#.........#.......#.......#3........#
+#####.#.###.#.#.###.#####.#.#.#.###.#.#.#####.#.#.###.#.#.###.#.###.#.#.#.#.#.#.#.#.#.#.#.###.#.#####.#.#.#.#.#######.###.#.#####.#.#.#.#####.#.#.#######.#.###.###.#.#####.#.#.#.#.#
+#.#...#...#...#.......#.....#...#.#...............#.#...#...#.#.....#.#.....#.#.#...#...#.........#.....#...#...........#.#.#.........#...........#...................#.....#.#.....#
+#.#.###.#.#.#.#.#.###.#.#.#.#.#.#.#.#####.#.#######.#.###.###############.#.###.#.#.#.#.#####.###.#.#####.#.#.#.#.#.###.#.#.#.#.###.#.#.###.#.#####.#.#.#####.#########.#####.###.###
+#.#.#.....#.....#...#...#...#.....#.........#.#...#...#.........#...#...#.......#.............#.............#.#.....#.#...#.#.......#...#.#.#...#.....#.#.......#...#...#.....#...#.#
+#.#.#.#.#.#######.#.#.###.###.###.###.#.###.#.#.#.#.#.#.#.#.###.#.#.#########.#.#.###.###.#.#.#.#.#.#.#######.#.#####.#.#.#######.#.#.#.#.###.#.#.###.#.#####.#.#.#.#.#.#.#######.#.#
+#.#.#.#...#...........#.#.....#.#...#.#.#.#.......#.#.....#...#.#...#.#.#...#...#.#.#.......#...#.#.....#.........#.....#.....#.#.....#...#.#.....#...#...#.......#.....#.....#.....#
+#.#.#.###.#.#.###.###.#.#.#.#.#.#######.#.###.#####.###.#.#.#.###.#.#.#.###.#.###.#.###.#.###.#.#.#.###.#.#.#####.###.###.#.###.###.#.#.###.#####.#.#.###.###.###.#.###.###.#.#.#.#.#
+#...#.................#.............#...#...#.......#.....#.#.#.........#...#...#.......#.#...#.....#...#.#.......#.#.......#.#...#.#.#.............#.....#...#.#...#.....#.#.#.....#
+#.#.#####.#.#.#.###.#.###.#########.#.#.###.#.#.###.#.###.#.###.#####.#.#.#.#.#.#.#####.#.#.#.#####.#.#.#######.#.#.#.#####.#.#.#.#####.#.#.#.###.#####.#.#.###.#.#.#.###.#.###.#.#.#
+#5....#...#.#...........#...#...#.#.#.#...#.#.#.....#...........#...#.#.......#.....#...#.........#.#.#.#.......#.#...#...#...#.#.#........2#...#.#...#...#.#...#.#...#.......#...#.#
+#####################################################################################################################################################################################

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

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

+ 39 - 0
D24.2/Program.cs

@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using static D24._1.Program;
+
+namespace D24._2
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+
+            var file = File.ReadAllText(args[0]).Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
+
+            var poi = new Dictionary<int, (int x, int y)>();
+
+            var map = ParseMap(file, poi);
+
+            var perm = GetPermutations(poi.Keys.OrderBy(p => p).Skip(1), poi.Count - 1);
+
+            int minStep = int.MaxValue;
+            foreach (var p in perm)
+            {
+                var current = 0;
+                int step = 0;
+                foreach (var destination in p.Append(0))
+                {
+                    step += Solve(map, poi[current], poi[destination]);
+                    current = destination;
+                }
+
+                if (step < minStep) minStep = step;
+            }
+
+            Console.WriteLine($"The answer is : {minStep}");
+        }
+    }
+}

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

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