예제 #1
0
        public static long part2()
        {
            init();
            bool[,] matrixfixed = new bool[50, 50];

            Stack <point> _path      = new Stack <point>();
            point         _lastpoint = new point()
            {
                x = 1, y = 1
            };

            matrixpath[_lastpoint.y, _lastpoint.x]  = true;
            matrixfixed[_lastpoint.y, _lastpoint.x] = true;
            _path.Push(_lastpoint);

            int moves = 0;

            do
            {
                double angle = Math.Atan2(50 - _lastpoint.y, 50 - _lastpoint.x);
                _lastpoint.setDirection(angle);
                point _newpoint;
                if (_lastpoint.goNext(out _newpoint) && _path.Count <= 50)
                {
                    _path.Push(_newpoint);
                    _lastpoint = _newpoint;
                    matrixpath[_lastpoint.y, _lastpoint.x] = matrixfixed[_lastpoint.y, _lastpoint.x] = true;
                }
                else
                {
                    matrixpath[_lastpoint.y, _lastpoint.x] = false;
                    _path.Pop();
                    if (_path.Count > 0)
                    {
                        _lastpoint = _path.Peek();
                    }
                }
                moves++;
            } while (_path.Count > 0);
            draw(_lastpoint.x, _lastpoint.y, matrixfixed);

            int count = 0;

            for (int y = 0; y < matrix.GetLength(0); y++)
            {
                for (int x = 0; x < matrix.GetLength(1); x++)
                {
                    if (matrixfixed[y, x])
                    {
                        count++;
                    }
                }
            }
            Console.WriteLine("Ended in {0} moves", moves);

            return(count);
        }
예제 #2
0
        public static int part1(bool debug)
        {
            init();
            Stack <point> _path      = new Stack <point>();
            point         _lastpoint = new point()
            {
                x = 1, y = 1
            };

            matrixpath[_lastpoint.y, _lastpoint.x] = true;
            _path.Push(_lastpoint);

            int moves = 0;

            do
            {
                draw(_lastpoint.x, _lastpoint.y, matrixpath);
                var keypressed = Console.ReadKey();
                switch (keypressed.Key)
                {
                case ConsoleKey.Q:
                    return(-1);

                case ConsoleKey.Enter:
                    double angle = Math.Atan2(pointY - _lastpoint.y, pointX - _lastpoint.x);
                    _lastpoint.setDirection(angle);
                    point _newpoint;
                    if (_lastpoint.goNext(out _newpoint))
                    {
                        _path.Push(_newpoint);
                        _lastpoint = _newpoint;
                        matrixpath[_lastpoint.y, _lastpoint.x] = true;
                    }
                    else
                    {
                        matrixpath[_lastpoint.y, _lastpoint.x] = false;
                        _path.Pop();
                        _lastpoint = _path.Peek();
                    }
                    moves++;
                    break;

                case ConsoleKey.Backspace:
                    matrixpath[_lastpoint.y, _lastpoint.x] = false;
                    _path.Pop();
                    _lastpoint = _path.Peek();
                    break;
                }
            } while ((_lastpoint.x != pointX || _lastpoint.y != pointY) && _path.Count > 0);
            Console.WriteLine("Ended in {0} moves", moves);
            return(_path.Count);
        }