Ejemplo n.º 1
0
        public static int Part2Solution(string[] lines)
        {
            string[] firstWireInput   = lines[0].Split(",");
            string[] secondWireOutput = lines[1].Split(",");

            List <GridMark> firstWireMarks  = CreateGridMarks(firstWireInput);
            List <GridMark> secondWireMarks = CreateGridMarks(secondWireOutput);

            var intersections = FetchIntersections(firstWireMarks, secondWireMarks);
            int lessSteps     = 0;

            foreach (var intersection in intersections)
            {
                GridMark mark1 = firstWireMarks.First(item => item.X == intersection.X && item.Y == intersection.Y);
                GridMark mark2 = secondWireMarks.First(item => item.X == intersection.X && item.Y == intersection.Y);

                if (lessSteps == 0)
                {
                    lessSteps = mark1.Steps + mark2.Steps;
                }
                else
                {
                    lessSteps = lessSteps > mark1.Steps + mark2.Steps ? mark1.Steps + mark2.Steps : lessSteps;
                }
            }

            return(lessSteps);
        }
Ejemplo n.º 2
0
        private static List <GridMark> CreateGridMarks(string[] wireInput)
        {
            List <GridMark> wireMarks = new List <GridMark>();

            GridMark currentPosition = new GridMark(0, 0, 0);

            foreach (var instruction in wireInput)
            {
                char direction = instruction[0];
                int  nSteps    = int.Parse(instruction.Substring(1));

                switch (direction)
                {
                case 'R':
                    for (var i = 0; i < nSteps; i++)
                    {
                        currentPosition.X++;
                        currentPosition.Steps++;
                        wireMarks.Add(currentPosition.Clone());
                    }
                    break;

                case 'U':
                    for (var i = 0; i < nSteps; i++)
                    {
                        currentPosition.Y++;
                        currentPosition.Steps++;
                        wireMarks.Add(currentPosition.Clone());
                    }
                    break;

                case 'L':
                    for (var i = 0; i < nSteps; i++)
                    {
                        currentPosition.X--;
                        currentPosition.Steps++;
                        wireMarks.Add(currentPosition.Clone());
                    }
                    break;

                case 'D':
                    for (var i = 0; i < nSteps; i++)
                    {
                        currentPosition.Y--;
                        currentPosition.Steps++;
                        wireMarks.Add(currentPosition.Clone());
                    }
                    break;
                }
            }

            return(wireMarks);
        }