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

+ 6 - 0
Adv2015.sln

@@ -35,6 +35,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D08.1", "D08.1\D08.1.csproj
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D08.2", "D08.2\D08.2.csproj", "{FBDA595A-9E1A-40DF-BC1D-C2393DF69F51}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D09", "D09.1\D09.csproj", "{8680B3A9-FBC7-4432-857A-9FE81ED4590B}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -105,6 +107,10 @@ Global
 		{FBDA595A-9E1A-40DF-BC1D-C2393DF69F51}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{FBDA595A-9E1A-40DF-BC1D-C2393DF69F51}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{FBDA595A-9E1A-40DF-BC1D-C2393DF69F51}.Release|Any CPU.Build.0 = Release|Any CPU
+		{8680B3A9-FBC7-4432-857A-9FE81ED4590B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{8680B3A9-FBC7-4432-857A-9FE81ED4590B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{8680B3A9-FBC7-4432-857A-9FE81ED4590B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{8680B3A9-FBC7-4432-857A-9FE81ED4590B}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 9 - 0
D09.1/D09.csproj

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

+ 117 - 0
D09.1/Program.cs

@@ -0,0 +1,117 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace D09._1
+{
+    public class VisitedList
+    {
+        public Place Visited;
+        private VisitedList Next;
+        public VisitedList(VisitedList list) => Next = list;
+        public bool Contains(Place place)
+        {
+            if (Visited.Equals(place)) return true;
+            return Next?.Contains(place) ?? false;
+        }
+        public int Count() => 1 + Next?.Count() ?? 1;
+    }
+
+    public class Place
+    {
+        public string Name { get; }
+
+        public Place(string name) => Name = name;
+
+        public HashSet<(int distance, Place neighbor)> Neighbors = new HashSet<(int distance, Place neighbor)>();
+
+        public override bool Equals(object obj)
+        {
+            var place = obj as Place;
+            return place != null &&
+                   Name == place.Name;
+        }
+
+        public override int GetHashCode()
+        {
+            return HashCode.Combine(Name);
+        }
+    }
+
+    class Program
+    {
+        static (int best, int worst) Test(Place start, VisitedList visited, int traveled, int totalPlaces)
+        {
+            if (visited.Count() == totalPlaces)
+                return (traveled, traveled);
+
+            int best = -1, worst = -1;
+            foreach ((int distance, Place neighbor) in start.Neighbors)
+            {
+                if (visited.Contains(neighbor)) continue;
+
+                (int b, int w) = Test(neighbor, new VisitedList(visited) { Visited = start }, traveled + distance, totalPlaces);
+                if (best == -1 || b < best) best = b;
+                if (w > worst) worst = w;
+            }
+
+            return (best, worst);
+        }
+
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) throw new ArgumentException();
+            if (File.Exists(args[0]) == false) throw new FileNotFoundException();
+
+            Dictionary<string, Place> places = new Dictionary<string, Place>();
+
+            ParseFile(args, places);
+
+            int best = -1, worst = -1;
+            foreach ((string name, Place start) in places)
+            {
+                (int b, int w) = Test(start, new VisitedList(null) { Visited = start }, 0, places.Count);
+                if (best == -1 || b < best) best = b;
+                if (w > worst) worst = w;
+            }
+
+            Console.WriteLine($"The best distance is : {best}");
+            Console.WriteLine($"The worst distance is : {worst}");
+        }
+
+        private static void ParseFile(string[] args, Dictionary<string, Place> places)
+        {
+            using (var file = File.OpenText(args[0]))
+            {
+                while (true)
+                {
+                    var line = file.ReadLine();
+                    if (line == null) break;
+
+                    var spl = line.Split(" = ");
+                    var ft = spl[0].Split(" to ");
+
+                    string from = ft[0], to = ft[1];
+                    int distance = int.Parse(spl[1]);
+
+                    places.TryGetValue(from, out var placeFrom);
+                    places.TryGetValue(to, out var placeTo);
+
+                    if (placeFrom == null)
+                    {
+                        placeFrom = new Place(from);
+                        places.Add(from, placeFrom);
+                    }
+                    if (placeTo == null)
+                    {
+                        placeTo = new Place(to);
+                        places.Add(to, placeTo);
+                    }
+
+                    placeFrom.Neighbors.Add((distance, placeTo));
+                    placeTo.Neighbors.Add((distance, placeFrom));
+                }
+            }
+        }
+    }
+}

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

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

+ 28 - 0
D09.1/input.txt

@@ -0,0 +1,28 @@
+AlphaCentauri to Snowdin = 66
+AlphaCentauri to Tambi = 28
+AlphaCentauri to Faerun = 60
+AlphaCentauri to Norrath = 34
+AlphaCentauri to Straylight = 34
+AlphaCentauri to Tristram = 3
+AlphaCentauri to Arbre = 108
+Snowdin to Tambi = 22
+Snowdin to Faerun = 12
+Snowdin to Norrath = 91
+Snowdin to Straylight = 121
+Snowdin to Tristram = 111
+Snowdin to Arbre = 71
+Tambi to Faerun = 39
+Tambi to Norrath = 113
+Tambi to Straylight = 130
+Tambi to Tristram = 35
+Tambi to Arbre = 40
+Faerun to Norrath = 63
+Faerun to Straylight = 21
+Faerun to Tristram = 57
+Faerun to Arbre = 83
+Norrath to Straylight = 9
+Norrath to Tristram = 50
+Norrath to Arbre = 60
+Straylight to Tristram = 27
+Straylight to Arbre = 81
+Tristram to Arbre = 90