Exemple #1
0
        public long Part1()
        {
            PaintingController paintingController = new PaintingController();
            IntCodeComputer    computer           = new IntCodeComputer(paintingController, paintingController);

            computer.Execute(_program);
            return(paintingController.GetPaintedPanels().Count);
        }
Exemple #2
0
        public string Part2()
        {
            PaintingController paintingController = new PaintingController(Color.White);
            IntCodeComputer    computer           = new IntCodeComputer(paintingController, paintingController);

            computer.Execute(_program);
            var paintedPanels = paintingController.GetPaintedPanels();
            var allX          = paintedPanels.Select(panel => panel.point.X).ToList();
            var allY          = paintedPanels.Select(panel => panel.point.Y).ToList();
            var minX          = allX.Min();
            var maxX          = allX.Max();
            var width         = (maxX - minX) + 1;
            var minY          = allY.Min();
            var maxY          = allY.Max();
            var height        = (maxY - minY) + 1;
            var result        = (new string(' ', width *height)).ToCharArray();
            var result2       = new char[width, height];

            foreach ((Point point, Color color)paintedPanel in paintedPanels)
            {
                if (paintedPanel.color == Color.White)
                {
                    int index = (paintedPanel.point.X + (-1 * minX)) + ((paintedPanel.point.Y + (-1 * minY)) * width);
                    result[index] = '#';
                }
            }

            foreach ((Point point, Color color)paintedPanel in paintedPanels)
            {
                if (paintedPanel.color == Color.White)
                {
                    int index = (paintedPanel.point.X + (-1 * minX)) * (paintedPanel.point.Y + (-1 * minY));
                    int x     = paintedPanel.point.X + (-1 * minX);
                    int y     = paintedPanel.point.Y + (-1 * minY);
                    result2[x, y] = '#';
                }
            }


            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < result.Length; i++)
            {
                builder.Append(result[i]);
                if (i % width == 0)
                {
                    builder.Append(Environment.NewLine);
                }
            }

            StringBuilder builder2 = new StringBuilder();

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    builder2.Append(result2[x, y] == '#' ? '#' : ' ');
                }

                builder2.Append(Environment.NewLine);
            }

            return(builder.ToString());
        }