Пример #1
0
        static void Main()
        {
            var input = File.ReadAllText("../../../input.txt").ParseLongs();

            var sw = new Stopwatch();

            sw.Start();

            for (int i = 0; i < 100; i++)
            {
                laby = new Labyrinth(input);

                var search = new BreadthFirstSearch <Point>(EqualityComparer <Point> .Default,
                                                            Expander)
                {
                    PerformParallelSearch = false
                };

                var oxygen = search.FindFirst(laby.Origin, n => n == laby.OxygenPos);
                //Console.WriteLine($"Part 1: The droid need {oxygen.Length} steps.");

                var filled = search.FindLeafs(laby.OxygenPos);

                var longestPath = filled.Max(path => path.Length);
                //Console.WriteLine($"Part 2: The point furthest from the oxygen is {longestPath} steps away.");
            }
            sw.Stop();
            Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms.");

            Console.WriteLine("Labyrinth map:");
            Console.WriteLine(laby.ToString());

            _ = Console.ReadLine();
        }
Пример #2
0
        public void FindsFirstTarget()
        {
            var bfs    = new BreadthFirstSearch <int>(EqualityComparer <int> .Default, x => new[] { x * 2, x * 3 });
            var result = bfs.FindFirst(1, x => x == 192);

            Assert.AreEqual(1, result.Steps.First());
            Assert.AreEqual(192, result.Steps.Last());
            Assert.AreEqual(7, result.Length);
        }
Пример #3
0
        static void Main()
        {
            var input = File.ReadAllLines("../../../input.txt");

            var sw = new Stopwatch();

            sw.Start();

            //var items = input.Select(s => s.Split(')'));
            //var orbitedby = items.ToLookup(x => x[0], x => x[1]);
            //var orbits = items.ToDictionary(x => x[1], x => x[0]);

            //var search = new BreadthFirstSearch<string>(
            //    EqualityComparer<string>.Default,
            //    node => orbitedby[node].Concat(orbits.GetOrEmpty(node)))
            //{ PerformParallelSearch = false };

            //var depths = search.FindAll("COM", x => true);
            //var sum = depths.Sum(path => path.Length);
            //Console.WriteLine($"Part 1: The total number of orbits is {sum}.");

            //var path = search.FindFirst("SAN", x => x == "YOU");
            //Console.WriteLine($"Part 2: The route von Santa to you has {path.Length - 2} transfers.");

            var graph  = Graph.FromEdges(input, s => s.Split(')').ToTuple2());
            var search = new BreadthFirstSearch <GraphNode <string, string> >(
                graph.NodeComparer,
                node => node.Neighbors)
            {
                PerformParallelSearch = false
            };

            var depths = search.FindAll(graph.Nodes["COM"], x => true);
            var sum    = depths.Sum(path => path.Length);

            Console.WriteLine($"Part 1: The total number of orbits is {sum}.");

            var path = search.FindFirst(graph.Nodes["SAN"], x => x.Value == "YOU");

            Console.WriteLine($"Part 2: The route von Santa to you has {path.Length - 2} transfers.");

            sw.Stop();
            Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms.");
            _ = Console.ReadLine();
        }
Пример #4
0
        private static void Main(string[] args)
        {
            /*
             * The first floor contains a strontium generator, a strontium-compatible microchip, a plutonium generator, and a plutonium-compatible microchip.
             * The second floor contains a thulium generator, a ruthenium generator, a ruthenium-compatible microchip, a curium generator, and a curium-compatible microchip.
             * The third floor contains a thulium-compatible microchip.
             * The fourth floor contains nothing relevant.
             */
            var input = "1 2 1 3 1"; // Example

            input = "1 1 1 1 1 2 2 2 2 2 3";
            input = "1 1 1 1 1 2 2 2 2 2 3 1 1 1 1"; // Julius
            input = "1 1 1 1 1 2 3 2 2 2 2 1 1 1 1"; // Mariano
            var initialState = State.FromString(input);

            var sw = new Stopwatch();

            Console.WriteLine("Initial State:");
            Console.WriteLine(initialState);

            var comparer = new StateEquivalent();
            IPath <State, Transition> path = null;

            for (var i = 0; i < 10; i++)
            {
                sw.Start();
                var bfs = new BreadthFirstSearch <State, Transition>(comparer, s => s.GetPossibleSuccessorStates());
                path = bfs.FindFirst(initialState, n => n.IsFinalState());
                sw.Stop();

                var duration = sw.ElapsedMilliseconds / 1000f;
                Console.WriteLine($"It took {duration:0.000} seconds.");
            }

            Console.WriteLine("========================================");
            if (path != null)
            {
                Console.WriteLine($"Final state reached after {path.Length} steps :-)");
            }
            Console.ReadLine();
        }