Ejemplo n.º 1
0
        public static int PartOne()
        {
            var program = new IntCodeProgram11(@"..\..\..\..\..\data\13.txt");
            var input   = new Queue <long>();
            var output  = program.Run(input);
            var blocks  = new HashSet <Vector>();

            while (output.Count > 0)
            {
                var x    = output.Dequeue();
                var y    = output.Dequeue();
                var type = output.Dequeue();
                if (type == 2)
                {
                    blocks.Add(new Vector(x, y));
                }
            }
            return(blocks.Count);
        }
Ejemplo n.º 2
0
        public static int PartOne()
        {
            var grid             = new Dictionary <Vector, long>();
            var directions       = new Vector[] { new Vector(0, 1), new Vector(1, 0), new Vector(0, -1), new Vector(-1, 0) };
            var currentDirection = 0;
            var currentPosition  = new Vector(0, 0);
            var program          = new IntCodeProgram11(@"..\..\..\..\..\data\11.txt");

            grid.Add(currentPosition, 0);
            int paintedPanels = 0;

            while (program.State != IntCodeProgramState.FINISHED)
            {
                long val   = 0;
                bool found = grid.TryGetValue(currentPosition, out val);
                if (!found)
                {
                    grid.Add(currentPosition, 0);
                }
                var input = new Queue <long>();
                input.Enqueue(val);
                var output  = program.Run(input);
                var color   = output.Dequeue();
                var turning = output.Dequeue();
                grid.Remove(currentPosition);
                grid.Add(currentPosition, color);
                paintedPanels++;
                if (turning == 0)
                {
                    currentDirection = currentDirection - 1 < 0 ? directions.Length - 1 : currentDirection - 1;
                }
                else if (turning == 1)
                {
                    currentDirection = (currentDirection + 1) % directions.Length;
                }
                currentPosition = Vector.Add(currentPosition, directions[currentDirection]);
            }
            return(grid.Count);
        }
Ejemplo n.º 3
0
        public static long PartTwo()
        {
            var program = new IntCodeProgram11(@"..\..\..\..\..\data\13.txt");

            var  height           = 21;
            var  width            = 38;
            long score            = 0;
            long currentPaddlePos = 0;
            long currentBallPos   = 0;
            long lastBallPos      = 0;

            long[] game = new long[width * height];
            {
                var input = new Queue <long>();
                //input.Enqueue(2);

                var output = program.Run(input);
                while (output.Count > 0)
                {
                    var x    = output.Dequeue();
                    var y    = output.Dequeue();
                    var type = output.Dequeue();
                    if (x == -1 && y == 0)
                    {
                        score = type;
                    }
                    else
                    {
                        game[x + y * width] = type;
                    }
                    if (type == 3)
                    {
                        currentPaddlePos = x;
                    }
                    else if (type == 4)
                    {
                        currentBallPos = x;
                        lastBallPos    = x;
                    }
                }

                Print(game, score, width, height);
            }
            long nextInput = -1;

            while (ContainsBlocks(game))
            {
                var input = new Queue <long>();
                input.Enqueue(nextInput);
                var output = program.Run(input);
                while (output.Count > 0)
                {
                    var x    = output.Dequeue();
                    var y    = output.Dequeue();
                    var type = output.Dequeue();
                    if (x == -1 && y == 0)
                    {
                        score = type;
                    }
                    else
                    {
                        game[x + y * width] = type;
                    }
                    if (type == 3)
                    {
                        currentPaddlePos = x;
                    }
                    else if (type == 4)
                    {
                        currentBallPos = x;
                    }
                }
                Print(game, score, width, height);
                if (currentPaddlePos < currentBallPos)
                {
                    nextInput = 1;
                    //currentPaddlePos++;
                }
                else if (currentPaddlePos > currentBallPos)
                {
                    nextInput = -1;
                    //currentPaddlePos--;
                }
                else
                {
                    nextInput = 0;
                }
            }


            return(score);
        }
Ejemplo n.º 4
0
        public static int PartTwo()
        {
            var grid             = new Dictionary <Vector, long>();
            var directions       = new Vector[] { new Vector(0, 1), new Vector(1, 0), new Vector(0, -1), new Vector(-1, 0) };
            var currentDirection = 0;
            var currentPosition  = new Vector(0, 0);
            var program          = new IntCodeProgram11(@"..\..\..\..\..\data\11.txt");

            grid.Add(currentPosition, 1);
            int paintedPanels = 0;

            while (program.State != IntCodeProgramState.FINISHED)
            {
                long val   = 0;
                bool found = grid.TryGetValue(currentPosition, out val);
                if (!found)
                {
                    grid.Add(currentPosition, 0);
                }
                var input = new Queue <long>();
                input.Enqueue(val);
                var output  = program.Run(input);
                var color   = output.Dequeue();
                var turning = output.Dequeue();
                grid.Remove(currentPosition);
                grid.Add(currentPosition, color);
                paintedPanels++;
                if (turning == 0)
                {
                    currentDirection = currentDirection - 1 < 0 ? directions.Length - 1 : currentDirection - 1;
                }
                else if (turning == 1)
                {
                    currentDirection = (currentDirection + 1) % directions.Length;
                }
                currentPosition = Vector.Add(currentPosition, directions[currentDirection]);
            }
            var lowerLeft  = new Vector();
            var upperRight = new Vector();

            foreach (KeyValuePair <Vector, long> entry in grid)
            {
                lowerLeft.X  = Math.Min(entry.Key.X, lowerLeft.X);
                lowerLeft.Y  = Math.Min(entry.Key.Y, lowerLeft.Y);
                upperRight.X = Math.Max(entry.Key.X, upperRight.X);
                upperRight.Y = Math.Max(entry.Key.Y, upperRight.Y);
            }
            var width  = (int)(upperRight.X - lowerLeft.X) + 1;
            var height = (int)(upperRight.Y - lowerLeft.Y) + 1;

            long[,] drawing = new long[height, width];
            foreach (KeyValuePair <Vector, long> entry in grid)
            {
                drawing[(int)(entry.Key.Y - lowerLeft.Y), (int)(entry.Key.X - lowerLeft.X)] = entry.Value;
            }

            for (int y = height - 1; y >= 0; y--)
            {
                string line = "";
                for (int x = 0; x < width; x++)
                {
                    line += drawing[y, x] == 1 ? '#' : '.';
                }
                Console.WriteLine(line);
            }
            return(grid.Count);
        }