|
|
@@ -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));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|