예제 #1
0
        public void ProblemOne()
        {
            var tubes = ParseInput(Input);

            //Find start pos
            var position  = new Vector2i(0, 0);
            var steps     = 0;
            var direction = Direction.Down;

            for (int x = 0; x < tubes.GetLength(0); x++)
            {
                if (tubes[x, 0] == '|')
                {
                    position.X = x;
                    break;
                }
            }

            var letters = new List <char>();

            while (true)
            {
                var pre = position.Clone();
                var c   = tubes[position.X, position.Y];

                if (c == ' ')
                {
                    //end
                    _cashedSteps = steps;
                    var result = string.Join(string.Empty, letters);
                    Console.WriteLine(result);
                    return;
                }

                if (char.IsLetter(c))
                {
                    letters.Add(c);

                    if (direction == Direction.Down || direction == Direction.Up)
                    {
                        position.Y += direction == Direction.Down ? 1 : -1;
                    }

                    if (direction == Direction.Left || direction == Direction.Right)
                    {
                        position.X += direction == Direction.Right ? 1 : -1;
                    }

                    steps++;
                }

                //Look downwards or upwards
                if (c == '|')
                {
                    position.Y += direction == Direction.Down ? 1 : -1;
                    steps++;
                    var nextChar = tubes[position.X, position.Y];

                    if (nextChar == '-')
                    {
                        //Need to skip over another pipe
                        position.Y += direction == Direction.Down ? 1 : -1;
                        steps++;
                    }
                }

                if (c == '-')
                {
                    position.X += direction == Direction.Right ? 1 : -1;
                    steps++;
                    var nextChar = tubes[position.X, position.Y];

                    if (nextChar == '|')
                    {
                        //Need to skip over another pipe
                        position.X += direction == Direction.Right ? 1 : -1;
                        steps++;
                    }
                }

                if (c == '+')
                {
                    //Dont go in reverse
                    var reverseDirection = direction.RotateLeft().RotateLeft();
                    List <(Direction direction, Vector2i vector)> adjacent = _adjacentIndices.Where(i => i.direction != reverseDirection).Select(i => (i.direction, position.Add(i.vector))).ToList();

                    foreach (var p in adjacent)
                    {
                        if (p.vector.X > 0 && p.vector.Y > 0 && p.vector.X < tubes.GetLength(0) && p.vector.Y < tubes.GetLength(1) && tubes[p.vector.X, p.vector.Y] != ' ')
                        {
                            position  = p.vector;
                            direction = p.direction;
                            steps++;
                        }
                    }
                }

                if (pre.Equals(position))
                {
                    //not moved. Problem detected.
                    throw new Exception();
                }
            }
        }