コード例 #1
0
ファイル: Program.cs プロジェクト: Mark-Sims/AdventOfCode2019
        static void Main(string[] args)
        {
            var inputFile = Environment.CurrentDirectory + "//input.txt";

            // Input file should be a single line
            string[] wires = System.IO.File.ReadAllLines(inputFile);
            //wires[0] = "R75,D30,R83,U83,L12,D49,R71,U7,L72";
            //wires[1] = "U62,R66,U55,R34,D71,R55,D58,R83";
            List <MovementCommand> wire1 = ParseInputCommandLine(wires[0]);
            List <MovementCommand> wire2 = ParseInputCommandLine(wires[1]);

            GridDimensions dimensions = CalculateTotalRequiredGridSize(new List <List <MovementCommand> > {
                wire1, wire2
            });

            Console.WriteLine(dimensions);

            // Offset the starting position into the grid since we're not starting at an edge.
            int startingX = Math.Abs(dimensions.MinX);
            int startingY = Math.Abs(dimensions.MinY);

            GridManager gridManager = new GridManager(dimensions.DimensionX, dimensions.DimensionY, startingX, startingY);

            TraceWire(gridManager, new Point(startingX, startingY), wire1, 1);
            TraceWire(gridManager, new Point(startingX, startingY), wire2, 2);

            // Collect the points that intersect
            List <Point> intersectionPoints = new List <Point>();
            List <int>   stepDistance       = new List <int>();

            foreach (var cell in gridManager.Cells)
            {
                if (cell.Value.Count > 1)
                {
                    intersectionPoints.Add(cell.Key);

                    int totalDistance = 0;
                    foreach (var wire in cell.Value)
                    {
                        totalDistance += wire.Steps;
                    }

                    // Again, forget about the middle point. It doesn't intersect with itself.
                    if (totalDistance != 0)
                    {
                        stepDistance.Add(totalDistance);
                    }
                }
            }

            Console.WriteLine(stepDistance.Min());
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Mark-Sims/AdventOfCode2019
        public static GridDimensions CalculateRequiredGridSizeForWire(List <MovementCommand> wireMovements)
        {
            //
            //     ^
            //     |
            //     |   // Positive directions
            //     |
            //     +--------->
            //

            GridDimensions grid = new GridDimensions();

            int currentX, currentY;

            currentX = currentY = 0;

            foreach (MovementCommand command in wireMovements)
            {
                switch (command.Direction)
                {
                case Direction.Up:
                    currentY += command.Distance;
                    grid.MaxY = Math.Max(grid.MaxY, currentY);
                    break;

                case Direction.Right:
                    currentX += command.Distance;
                    grid.MaxX = Math.Max(grid.MaxX, currentX);
                    break;

                case Direction.Down:
                    currentY -= command.Distance;
                    grid.MinY = Math.Min(grid.MinY, currentY);
                    break;

                case Direction.Left:
                    currentX -= command.Distance;
                    grid.MinX = Math.Min(grid.MinX, currentX);
                    break;
                }
            }
            return(grid);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: Mark-Sims/AdventOfCode2019
        public static GridDimensions CalculateTotalRequiredGridSize(List <List <MovementCommand> > wires)
        {
            List <MovementCommand> wire1 = wires[0];
            List <MovementCommand> wire2 = wires[1];

            GridDimensions requiredGridWire1 = CalculateRequiredGridSizeForWire(wire1);
            GridDimensions requiredGridWire2 = CalculateRequiredGridSizeForWire(wire2);

            GridDimensions totalGrid = new GridDimensions()
            {
                MinX = Math.Min(requiredGridWire1.MinX, requiredGridWire2.MinX),
                MaxX = Math.Max(requiredGridWire1.MaxX, requiredGridWire2.MaxX),
                MinY = Math.Min(requiredGridWire1.MinY, requiredGridWire2.MinY),
                MaxY = Math.Max(requiredGridWire1.MaxY, requiredGridWire2.MaxY),
            };

            totalGrid.DimensionX = Math.Abs(totalGrid.MinX) + totalGrid.MaxX + 1;
            totalGrid.DimensionY = Math.Abs(totalGrid.MinY) + totalGrid.MaxY + 1;

            return(totalGrid);
        }