Esempio n. 1
0
        private static string Render(Dictionary <Point, int> canvas)
        {
            var whitePoints = canvas.Where(x => x.Value == 1).Select(x => x.Key).ToList();
            var topLeft     = new Point(whitePoints.Min(x => x.X), whitePoints.Min(x => x.Y));
            var bottomRight = new Point(whitePoints.Max(x => x.X), whitePoints.Max(x => x.Y));
            var resultSb    = new StringBuilder();

            for (var x = topLeft.X; x <= bottomRight.X; x++)
            {
                for (var y = topLeft.Y; y <= bottomRight.Y; y++)
                {
                    if (canvas.TryGetValue(new Point(x, y), out var color))
                    {
                        resultSb.Append(color == 0 ? ' ' : '#');
                    }
                    else
                    {
                        resultSb.Append(' ');
                    }
                }
                resultSb.AppendLine();
            }

            return(resultSb.ToString());
        }
Esempio n. 2
0
        private async Task <int> Paint(SynchronousIntMachine intMachine, Dictionary <Point, int> canvas)
        {
            var paintedPositions = new HashSet <Point> ();
            var directions       = new [] { new Point(-1, 0), new Point(0, 1), new Point(1, 0), new Point(0, -1) };
            var direction        = 0;
            var pos = new Point(0, 0);

            while (intMachine.RunUntilBlockOrComplete() != SynchronousIntMachine.ReturnCode.Completed)
            {
                var color = canvas.GetOrAdd(pos, _ => 0);
                intMachine.InputQueue.Enqueue(color);

                intMachine.RunUntilBlockOrComplete();
                canvas [pos] = (int)intMachine.OutputQueue.Dequeue();
                paintedPositions.Add(pos);

                intMachine.RunUntilBlockOrComplete();
                var directionDelta = (int)intMachine.OutputQueue.Dequeue() == 0 ? -1 : 1;
                direction = (direction + directionDelta + 4) % 4;
                pos      += directions [direction];
            }

            return(paintedPositions.Count);
        }
Esempio n. 3
0
        public static async Task Run()
        {
            var input = await File.ReadAllTextAsync("inputs\\13.txt");

            Dictionary <PointInt, tile_type> tiles = new Dictionary <PointInt, tile_type> ();

            var intMachine = new SynchronousIntMachine(input);

            while (intMachine.RunUntilBlockOrComplete() == ReturnCode.WrittenOutput)
            {
                while (intMachine.OutputQueue.Count < 3)
                {
                    intMachine.RunUntilBlockOrComplete();
                }
                var x    = (int)intMachine.OutputQueue.Dequeue();
                var y    = (int)intMachine.OutputQueue.Dequeue();
                var tile = intMachine.OutputQueue.Dequeue();

                tiles [new PointInt(x, y)] = (tile_type)tile;
            }

            var blocks = tiles.Where(e => e.Value == tile_type.Block).Count();

            Console.WriteLine("Part 1: " + blocks.ToString());

            intMachine = new SynchronousIntMachine(input);

            intMachine.SetMemoryRegister(0, 2);

            long       score      = 0;
            var        blockCount = blocks;
            var        ball       = Classes.Point.Empty;
            var        paddle     = Classes.Point.Empty;
            ReturnCode returnCode;

            while (blockCount > -1 && (returnCode = intMachine.RunUntilBlockOrComplete()) != ReturnCode.Completed)
            {
                switch (returnCode)
                {
                case ReturnCode.WaitingForInput:
                    if (blockCount == 0)
                    {
                        blockCount = -1; break;
                    }
                    var joystickInput = ball.X.CompareTo(paddle.X);
                    intMachine.InputQueue.Enqueue(joystickInput);
                    break;

                case ReturnCode.WrittenOutput:
                    while (intMachine.OutputQueue.Count < 3)
                    {
                        intMachine.RunUntilBlockOrComplete();
                    }
                    var x = (int)intMachine.OutputQueue.Dequeue();
                    var y = (int)intMachine.OutputQueue.Dequeue();
                    var t = intMachine.OutputQueue.Dequeue();

                    if (x == -1)
                    {
                        score = t;
                    }
                    else
                    {
                        var tile = (tile_type)t;
                        if (tile != tile_type.Block && tiles [new PointInt(x, y)] == tile_type.Block)
                        {
                            blockCount--;
                        }
                        tiles [new PointInt(x, y)] = tile;

                        if (tile == tile_type.Ball)
                        {
                            ball = new Classes.Point(x, y);
                        }
                        else if (tile == tile_type.HorizPaddle)
                        {
                            paddle = new Classes.Point(x, y);
                        }
                    }
                    break;
                }
            }

            Console.WriteLine("Part 2: " + score);
        }