示例#1
0
        private static void SolveElevator3(ElevatorState start)
        {
            Queue <ElevatorState>                     q      = new Queue <ElevatorState>();
            HashSet <ElevatorState>                   closed = new HashSet <ElevatorState>();
            Dictionary <ElevatorState, int>           dist   = new Dictionary <ElevatorState, int>();
            Dictionary <ElevatorState, ElevatorState> prev   = new Dictionary <ElevatorState, ElevatorState>();

            dist[start] = 0;
            q.Enqueue(start);
            int       checks = 0;
            Stopwatch timer  = Stopwatch.StartNew();

            while (true)
            {
                checks++;
                var u = q.Dequeue();
                if (closed.Contains(u))
                {
                    continue;
                }

                if (timer.Elapsed.TotalSeconds > 1)
                {
                    Console.CursorTop = 0;
                    Console.WriteLine("{0} checks/s", checks);
                    checks = 0;
                    timer  = Stopwatch.StartNew();
                    PrintState(u);
                }

                closed.Add(u);

                if (u.IsDone())
                {
                    int count = 0;

                    foreach (var item in EnumeratePath(prev, u).Reverse())
                    {
                        count++;
                        PrintState(item);
                    }

                    Console.WriteLine("Total steps {0}", count);
                    return;
                }

                foreach (var v in EnumerateValidMoves(u))
                {
                    q.Enqueue(v);
                    var alt = dist[u] + 1;
                    int newDist;

                    if (alt < (dist.TryGetValue(v, out newDist) ? newDist : int.MaxValue))
                    {
                        dist[v] = alt;
                        prev[v] = u;
                    }
                }
            }
        }
示例#2
0
        private static void PrintState(ElevatorState state)
        {
            if (frameLimit++ % 1000 != 0)
            {
                //return;
            }

            if (timer == null)
            {
                timer = Stopwatch.StartNew();
            }

            //Console.CursorTop = 20;
            for (int i = 3; i >= 0; i--)
            {
                Console.Write("Fl{0}|", i);
                foreach (var item in state.Keys)
                {
                    Console.Write("{0,5}", (state[item] == i) ? item.ToString().Substring(0, Math.Min(item.ToString().Length, 3)) : string.Empty);
                }

                Console.WriteLine();
            }

            timer.Restart();
        }
示例#3
0
        private static void Day11()
        {
            var state = new ElevatorState();

            //The first floor contains a thulium generator, a thulium-compatible microchip, a plutonium generator, and a strontium generator.
            //The second floor contains a plutonium-compatible microchip and a strontium-compatible microchip.
            //The third floor contains a promethium generator, a promethium-compatible microchip, a ruthenium generator, and a ruthenium - compatible microchip.
            //The fourth floor contains nothing relevant.
            state[Elements.Human] = 0;
            state[Elements.TmGen] = 0;
            state[Elements.Tm]    = 0;
            state[Elements.PuGen] = 0;
            state[Elements.SrGen] = 0;

            state[Elements.Pu] = 1;
            state[Elements.Sr] = 1;

            state[Elements.PmGen] = 2;
            state[Elements.Pm]    = 2;
            state[Elements.RuGen] = 2;
            state[Elements.Ru]    = 2;

            state[Elements.ElGen] = 0;
            state[Elements.El]    = 0;
            state[Elements.LiGen] = 0;
            state[Elements.Li]    = 0;

            SolveElevator3(state);
            //Console.WriteLine("Min moves {0}", SolveElevator(history));
        }
示例#4
0
        private static IEnumerable <ElevatorState> EnumerateValidMoves(ElevatorState currentState)
        {
            int myFloor = currentState[Elements.Human];

            if (myFloor < 3)
            {
                // Going up
                var newFloor = myFloor + 1;
                foreach (var newState in currentState.EnumerateMoves(myFloor, newFloor))
                {
                    yield return(newState);
                }
            }

            if (myFloor > 0)
            {
                // Going down
                var newFloor = myFloor - 1;
                foreach (var newState in currentState.EnumerateMoves(myFloor, newFloor))
                {
                    yield return(newState);
                }
            }
        }