Esempio n. 1
0
        public static void PartOne()
        {
            long[]       data          = InputHelper.GetIntcodeFromFile("11");
            List <Panel> panelsPainted = new List <Panel>();

            IntcodeComputer robot   = new IntcodeComputer(data, IntcodeMode.Blocking | IntcodeMode.Quiet);
            Thread          running = new Thread(() => robot.Run());

            running.Start();
            int       x      = 0;
            int       y      = 0;
            Direction facing = Direction.Up;

            while (!robot.IsFinished)
            {
                Panel panelAtXY = panelsPainted.FirstOrDefault(p => p.X == x && p.Y == y);
                if (panelAtXY == null)
                {
                    robot.InputQueue.Enqueue(0);
                }
                else
                {
                    robot.InputQueue.Enqueue(panelAtXY.Color == Color.White ? 1 : 0);
                }
                while (!robot.IsFinished && robot.OutputQueue.Count == 0)
                {
                    ;
                }
                if (robot.IsFinished)
                {
                    break;
                }
                long paint = robot.OutputQueue.Dequeue();
                if (paint == 1)
                {
                    if (panelAtXY != null)
                    {
                        panelAtXY.Color = Color.White;
                    }
                    else
                    {
                        panelsPainted.Add(new Panel(x, y, Color.White));
                    }
                }
                else
                {
                    if (panelAtXY != null)
                    {
                        panelAtXY.Color = Color.Black;
                    }
                    else
                    {
                        panelsPainted.Add(new Panel(x, y, Color.Black));
                    }
                }
                while (robot.OutputQueue.Count == 0)
                {
                    ;
                }
                long action = robot.OutputQueue.Dequeue();
                if (action == 1)
                {
                    facing = TurnRight(facing);
                }
                else
                {
                    facing = TurnLeft(facing);
                }
                switch (facing)
                {
                case Direction.Up:
                    y--;
                    break;

                case Direction.Left:
                    x--;
                    break;

                case Direction.Down:
                    y++;
                    break;

                case Direction.Right:
                    x++;
                    break;
                }
            }
            Console.WriteLine("Robot finished painting !");
            for (y = panelsPainted.Min(p => p.Y); y < panelsPainted.Max(p => p.Y); y++)
            {
                for (x = panelsPainted.Min(p => p.X); x < panelsPainted.Max(p => p.X); x++)
                {
                    Panel panelAtXY = panelsPainted.FirstOrDefault(p => p.X == x && p.Y == y);
                    if (panelAtXY == null || panelAtXY.Color == Color.Black)
                    {
                        Console.Write('.');
                    }
                    else
                    {
                        Console.Write('#');
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine($"{panelsPainted.Count} panels were painted !");
        }