Beispiel #1
0
        public void Examples(int from, int to, int steps)
        {
            var lines       = File.ReadAllLines($"{TestContext.CurrentContext.TestDirectory}\\testinput.txt");
            var routeFinder = new RouteFinder(lines);
            var minDist     = routeFinder.GetDistance(routeFinder.Targets[from], routeFinder.Targets[to]);

            Assert.AreEqual(steps, minDist);
        }
Beispiel #2
0
        public void TestGetDist(int from, int to)
        {
            stopwatch.Start();
            var lines       = File.ReadAllLines($"{TestContext.CurrentContext.TestDirectory}\\input.txt");
            var routeFinder = new RouteFinder(lines);
            var minDist     = routeFinder.GetDistance(routeFinder.Targets[from], routeFinder.Targets[to]);

            Console.Out.WriteLine(minDist);
            stopwatch.Stop();
        }
Beispiel #3
0
        public void OutputCombinations()
        {
            var combinations = new List <Tuple <int, int> >();
            var distances    = new Dictionary <Tuple <int, int>, int>();
            var lines        = File.ReadAllLines($"{TestContext.CurrentContext.TestDirectory}\\input.txt");
            var routeFinder  = new RouteFinder(lines);

            LogManager.GetCurrentClassLogger().Info("==============Start");
            for (int y = 0; y < 8; y++)
            {
                for (int x = y + 1; x < 8; x++)
                {
                    var pair = new Tuple <int, int>(y, x);
                    var dist = routeFinder.GetDistance(routeFinder.Targets[pair.Item1], routeFinder.Targets[pair.Item2]);
                    LogManager.GetCurrentClassLogger().Info($"{y} -> {x} = {dist}");
                    distances.Add(Tuple.Create(y, x), dist);
                    distances.Add(Tuple.Create(x, y), dist); //symmetrical?!
                }
            }

            int bestTourDist = int.MaxValue;

            int[] bestTour = null;
            GetAllPossibleTours(ints =>
            {
                if (ints[0] == 0)
                {
                    int tourDist = 0;
                    for (int i = 1; i < 8; i++)
                    {
                        tourDist += distances[Tuple.Create(ints[i - 1], ints[i])];
                    }
                    tourDist += distances[Tuple.Create(ints.Last(), 0)];
                    if (tourDist < bestTourDist)
                    {
                        bestTourDist = tourDist;
                        bestTour     = ints.ToArray();
                    }
                }
            });
            LogManager.GetCurrentClassLogger().Info($"Best tour = {string.Join("-", bestTour.Select(i => i.ToString()))}, distance = {bestTourDist}");

            Console.Out.WriteLine(combinations.Count);
        }