Exemplo n.º 1
0
        private Status CheckCurrentPoint(DroidPoint currentPoint, Droid droid)
        {
            var statusResult = droid.Move(currentPoint);

            droid.ReturnHome();

            return(statusResult);
        }
Exemplo n.º 2
0
        public string CalculatePart1(string inputFile)
        {
            var program = GetProgram(inputFile);
            var droid   = new Droid(program);

            var startPoint = new DroidPoint(0, 0, null);
            var map        = new Dictionary <DroidPoint, Status>();

            map.Add(startPoint, Status.Empty);

            var queue = new Queue <DroidPoint>();

            EnqueuePoints(startPoint, Movement.East);
            EnqueuePoints(startPoint, Movement.West);
            EnqueuePoints(startPoint, Movement.North);
            EnqueuePoints(startPoint, Movement.South);

            while (queue.Any())
            {
                var currentPoint = queue.Dequeue();
                if (map.ContainsKey(currentPoint))
                {
                    continue;
                }

                var status = CheckCurrentPoint(currentPoint, droid);
                map.Add(currentPoint, status);

                if (status == Status.Empty)
                {
                    EnqueuePoints(currentPoint, Movement.East);
                    EnqueuePoints(currentPoint, Movement.West);
                    EnqueuePoints(currentPoint, Movement.North);
                    EnqueuePoints(currentPoint, Movement.South);
                }

                else if (status == Status.Oxygen)
                {
                    return((currentPoint.HomePath.GetPathToHome().Count() + 1).ToString());
                }
            }

            return(0.ToString());

            void EnqueuePoints(DroidPoint currentPoint, Movement movement)
            {
                var newPoint = currentPoint.GetPoint(movement);

                if (!map.ContainsKey(newPoint))
                {
                    queue.Enqueue(newPoint);
                }
            }
        }