Esempio n. 1
0
        object PartOne(string input)
        {
            var painted   = new HashSet <Point>();
            var sif       = new Sif();
            var direction = Direction.Up;
            var location  = new Point(0, 0);
            var vm        = new IntCode(input);

            while (vm.State != ProgramState.Finished)
            {
                var currentColor = sif.GetColor(location);
                currentColor = currentColor == Sif.Color.Transparent ? Sif.Color.Black : currentColor;
                vm.ProvideInput((long)currentColor);
                vm.RunToNextInputOrFinished();

                // Get the output (if any)
                if (vm.HasOutput)
                {
                    var color = (Sif.Color)vm.ReadOutput();
                    sif.SetColor(location, color);
                    painted.Add(location);

                    var turn = vm.ReadOutput();
                    if (turn == 0)
                    {
                        // Turn left
                        direction = (Direction)(((int)direction - 1 + 4) % 4);
                    }
                    else
                    {
                        // Turn right
                        direction = (Direction)(((int)direction + 1) % 4);
                    }

                    switch (direction)
                    {
                    case Direction.Up:
                        location = new Point(location.X, location.Y - 1);
                        break;

                    case Direction.Right:
                        location = new Point(location.X + 1, location.Y);
                        break;

                    case Direction.Down:
                        location = new Point(location.X, location.Y + 1);
                        break;

                    case Direction.Left:
                        location = new Point(location.X - 1, location.Y);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }

            return(painted.Count);
        }
Esempio n. 2
0
        object PartTwo(string input)
        {
            var layersString = new List <string>();

            for (var index = 0; index < input.Length; index += 25 * 6)
            {
                layersString.Add(input.Substring(index, 25 * 6));
            }

            var finalImage = new StringBuilder();

            for (var index = 0; index < 25 * 6; index++)
            {
                var layersForThisPixel = layersString.Select(s => s[index]).ToArray();
                var color = layersForThisPixel.First(c => c != '2');
                finalImage.Append(color);
            }

            var text = finalImage.ToString();

            var sif = new Sif();

            for (var l = 0; l < 6; l++)
            {
                var line = text.Substring(25 * l, 25);
                for (var ci = 0; ci < 25; ci++)
                {
                    sif.SetColor(new Point(ci, l), line[ci] == '0' ? Sif.Color.Black : Sif.Color.White);
                }
            }

            sif.Draw(Console.Out);
            return(sif.OCR());

            //for (var l = 0; l < 6; l++)
            //{
            //    var line = text.Substring(25 * l, 25);
            //    var t = line.Replace('0', ' ');
            //    t = t.Replace('1', '█');
            //    Console.WriteLine(t);
            //}
            //
            //return "ZYBLH";
        }
Esempio n. 3
0
        object PartTwo(string input)
        {
            var sif       = new Sif();
            var direction = Direction.Up;
            var location  = new Point(0, 0);

            sif.SetColor(location, Sif.Color.White);
            var vm = new IntCode(input);

            while (vm.State != ProgramState.Finished)
            {
                var currentColor = sif.GetColor(location);
                var vmInput      = currentColor == Sif.Color.White ? 1 : 0;
                vm.ProvideInput(vmInput);
                vm.RunToNextInputOrFinished();

                // Get the output (if any)
                if (vm.HasOutput)
                {
                    var color = (Sif.Color)vm.ReadOutput();
                    sif.SetColor(location, color);

                    var turn = vm.ReadOutput();
                    if (turn == 0)
                    {
                        // Turn left
                        direction = (Direction)(((int)direction - 1 + 4) % 4);
                    }
                    else
                    {
                        // Turn right
                        direction = (Direction)(((int)direction + 1) % 4);
                    }

                    switch (direction)
                    {
                    case Direction.Up:
                        location = new Point(location.X, location.Y - 1);
                        break;

                    case Direction.Right:
                        location = new Point(location.X + 1, location.Y);
                        break;

                    case Direction.Down:
                        location = new Point(location.X, location.Y + 1);
                        break;

                    case Direction.Left:
                        location = new Point(location.X - 1, location.Y);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }

            // Gah. Should've trusted my instincts and made the SIF OCR.
            sif.Draw(Console.Out);
            return("JZPJRAGJ");
        }