Ejemplo n.º 1
0
        public string CalculatePart2(string inputFile)
        {
            var program = GetProgram(inputFile);

            var runner  = new IntCodeRunner9(program);
            var painted =
                new IntCodeRunner9.DefaultableDictionary <PanelPoint, long>(new Dictionary <PanelPoint, long>(), 0);


            var currentPoint = new PanelPoint(0, 0);

            painted[currentPoint] = 1;
            var currentDirection = Direction.Up;

            while (true)
            {
                var color = painted[currentPoint];

                var paint = runner.Run(color);
                var turn  = runner.Run(color);

                if (!paint.HasValue || !turn.HasValue)
                {
                    break;
                }

                painted[currentPoint] = paint.Value;
                if (turn == 0)
                {
                    currentDirection = TurnLeft(currentDirection);
                }
                else if (turn == 1)
                {
                    currentDirection = TurnRight(currentDirection);
                }

                currentPoint = currentPoint.GetPoint(currentDirection);
            }

            var whites = painted.Where(x => x.Value == 1).OrderByDescending(x => x.Key.Y).GroupBy(x => x.Key.Y);

            var minX = painted.Min(x => x.Key.X);
            var maxX = painted.Max(x => x.Key.X);

            var sb = new StringBuilder();

            sb.AppendLine();
            foreach (var whiteLine in whites)
            {
                for (int i = minX; i < maxX + 1; i++)
                {
                    sb.Append(whiteLine.Any(x => x.Key.X == i) ? "*" : " ");
                }

                sb.AppendLine();
            }

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        public string CalculatePart1(string inputFile)
        {
            var program = GetProgram(inputFile);

            var runner  = new IntCodeRunner9(program);
            var painted =
                new IntCodeRunner9.DefaultableDictionary <PanelPoint, long>(new Dictionary <PanelPoint, long>(), 0);


            var currentPoint     = new PanelPoint(0, 0);
            var currentDirection = Direction.Up;

            while (true)
            {
                var color = painted[currentPoint];

                var paint = runner.Run(color);
                var turn  = runner.Run(color);

                if (!paint.HasValue || !turn.HasValue)
                {
                    break;
                }

                painted[currentPoint] = paint.Value;
                if (turn == 0)
                {
                    currentDirection = TurnLeft(currentDirection);
                }
                else if (turn == 1)
                {
                    currentDirection = TurnRight(currentDirection);
                }

                currentPoint = currentPoint.GetPoint(currentDirection);
            }

            return(painted.Count.ToString());
        }