Example #1
0
        protected override object SolvePart1()
        {
            vm.ResetVM();
            vm.Execute(new long[] { 1 });

            return(vm.Output.Last());
        }
Example #2
0
        protected override object SolvePart1()
        {
            var vm     = new IntcodeVM(Input[0]);
            var result = vm.Execute();

            return(vm.Output.Where((t, i) => i % 3 == 2).Count(x => x == 2));
        }
Example #3
0
        protected override object SolvePart2()
        {
            var vm = new IntcodeVM(Input[0]);

            for (var noun = 0; noun < 100; noun++)
            {
                for (var verb = 0; verb < 100; verb++)
                {
                    vm.Write(1, noun);
                    vm.Write(2, verb);

                    vm.Execute();

                    if (vm.Read(0) != 19690720)
                    {
                        vm.ResetVM();
                        continue;
                    }

                    return(100 * noun + verb);
                }
            }

            return(null);
        }
Example #4
0
        protected override object SolvePart1()
        {
            var position  = (0, 0);
            var direction = RobotDirection.Up;
            var panels    = new Dictionary <(int, int), long>();
            var vm        = new IntcodeVM(Input[0]);

            var status = IntcodeVM.HaltMode.Unknown;

            while (status != IntcodeVM.HaltMode.Terminated)
            {
                var currentPanelColor = panels.ContainsKey(position) ? panels[position] : 0L;

                status = vm.Execute(new long[] { currentPanelColor });
                var panelColor = vm.Output.Dequeue();
                var turn       = (int)vm.Output.Dequeue();

                if (panels.ContainsKey(position))
                {
                    panels[position] = panelColor;
                }
                else
                {
                    panels.Add(position, panelColor);
                }

                direction = TurnRobot(direction, turn);

                position = MoveRobot(direction, position);
            }

            return(panels.Count);
        }
Example #5
0
        protected override object SolvePart1()
        {
            var vm = new IntcodeVM(Input[0]);

            vm.Execute(new long[] { 1 });

            return(vm.Output.First());
        }
Example #6
0
        protected override object SolvePart1()
        {
            var maxOutput     = 0L;
            var phaseSettings = new long[] { 0, 1, 2, 3, 4 };
            var vm            = new IntcodeVM(Input[0]);

            do
            {
                // Amp A
                vm.ResetVM();
                var input = new[] { phaseSettings[0], 0 };
                vm.Execute(input);
                var output = vm.Output.ToArray();

                // Amp B
                vm.ResetVM();
                input = new[] { phaseSettings[1], output[0] };
                vm.Execute(input);
                output = vm.Output.ToArray();

                // Amp C
                vm.ResetVM();
                input = new[] { phaseSettings[2], output[0] };
                vm.Execute(input);
                output = vm.Output.ToArray();

                // Amp D
                vm.ResetVM();
                input = new[] { phaseSettings[3], output[0] };
                vm.Execute(input);
                output = vm.Output.ToArray();

                // Amp E
                vm.ResetVM();
                input = new[] { phaseSettings[4], output[0] };
                vm.Execute(input);
                output = vm.Output.ToArray();

                if (output[0] > maxOutput)
                {
                    maxOutput = output[0];
                }
            } while (phaseSettings.NextPermutation());

            return(maxOutput);
        }
Example #7
0
        protected override object SolvePart1()
        {
            var vm = new IntcodeVM(Input[0]);

            vm.Write(1, 12);
            vm.Write(2, 2);
            vm.Execute();

            return(vm.Read(0));
        }
Example #8
0
        protected override object SolvePart1()
        {
            var map = new Dictionary <(int X, int Y), char>();
            var vm  = new IntcodeVM(Input[0]);

            var(x, y) = (0, 0);

            var vmStatus = vm.Execute();

            while (vm.Output.Count > 0)
            {
                var o = vm.Output.Dequeue();
                var c = (char)o;
                Console.Write(c);
                if (o == 10)
                {
                    (x, y) = (0, y + 1);
                }
                else
                {
                    map[(x++, y)] = c;
Example #9
0
        protected override object SolvePart2()
        {
            var vm = new IntcodeVM(Input[0]);

            vm.Write(0, 2);
            var paddle = (X : -1L, Y : -1L);
            var ball   = (X : -1L, Y : -1L);

            while (vm.Execute() != IntcodeVM.HaltMode.Terminated)
            {
                var(Paddle, Ball, _) = ProcessOutput(vm);

                if (Paddle.X != -1 && Paddle.Y != -1)
                {
                    paddle = Paddle;
                }
                if (Ball.X != -1 && Ball.Y != -1)
                {
                    ball = Ball;
                }

                if (ball.X < paddle.X)
                {
                    vm.Input.Enqueue(-1);
                }
                else if (ball.X > paddle.X)
                {
                    vm.Input.Enqueue(1);
                }
                else
                {
                    vm.Input.Enqueue(0);
                }
            }

            var finalState = ProcessOutput(vm);

            return(finalState.Score);
        }
Example #10
0
        protected override object SolvePart2()
        {
            var position  = (0, 0);
            var direction = RobotDirection.Up;
            var panels    = new Dictionary <(int, int), long>();
            var vm        = new IntcodeVM(Input[0]);

            panels.Add((0, 0), 1L);

            var status = IntcodeVM.HaltMode.Unknown;

            while (status != IntcodeVM.HaltMode.Terminated)
            {
                var currentPanelColor = panels.ContainsKey(position) ? panels[position] : 0L;

                status = vm.Execute(new long[] { currentPanelColor });
                var panelColor = vm.Output.Dequeue();
                var turn       = (int)vm.Output.Dequeue();

                if (panels.ContainsKey(position))
                {
                    panels[position] = panelColor;
                }
                else
                {
                    panels.Add(position, panelColor);
                }

                direction = TurnRobot(direction, turn);

                position = MoveRobot(direction, position);
            }

            var minX      = panels.Keys.Min(x => x.Item1);
            var minY      = panels.Keys.Min(y => y.Item2);
            var maxX      = panels.Keys.Max(x => x.Item1);
            var maxY      = panels.Keys.Max(y => y.Item2);
            var imgWidth  = maxX - minX + 20;
            var imgHeight = maxY - minY + 20;

            using var image = new Bitmap(imgWidth, imgHeight);
            for (var y = 0; y < imgHeight; y++)
            {
                for (var x = 0; x < imgWidth; x++)
                {
                    image.SetPixel(x, y, Color.Black);
                }
            }

            foreach (var panel in panels)
            {
                if (panel.Value == 1)
                {
                    image.SetPixel(panel.Key.Item1 - minX + 10, panel.Key.Item2 - minY + 10, Color.White);
                }
            }
            image.RotateFlip(RotateFlipType.Rotate270FlipNone);
            using var bigImage = new Bitmap(image, new Size(image.Width * 4, image.Height * 4));
            bigImage.Save(@".\2019\AdventOfCode2019112.png");

            using var engine = new TesseractEngine(@".\_ExternalDependencies\tessdata_legacy", "eng", EngineMode.TesseractOnly);
            using Pix pix    = PixConverter.ToPix(bigImage);
            using var page   = engine.Process(pix);
            return(page.GetText().Trim('\n'));
        }