コード例 #1
0
ファイル: IntComputer.cs プロジェクト: alexis2b/aoc2019
        public void Test1_3()
        {
            var program  = new long[] { 104, 1125899906842624, 99 };
            var computer = new IntComputer();

            computer.Run(program, new long[] {});
            Assert.AreEqual(1125899906842624L, computer.PopOutput());
        }
コード例 #2
0
ファイル: IntComputer.cs プロジェクト: alexis2b/aoc2019
        public void Test1_2()
        {
            var program  = new long[] { 1102, 34915192, 34915192, 7, 4, 7, 99, 0 };
            var computer = new IntComputer();

            computer.Run(program, new long[] {});
            var outputStr = computer.PopOutput().ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(16, outputStr.Length);
        }
コード例 #3
0
ファイル: IntComputer.cs プロジェクト: alexis2b/aoc2019
        public void TestWriteOutOfBounds()
        {
            var program = new long[]   {
                1, 7, 8, 1024, 4, 1024, 99, 321, 123
            };                                                        // add[7]+[8]->[1024], output [1024] -> expect [1024] to be 444 (321+123)
            var computer = new IntComputer();

            computer.Run(program);
            Assert.AreEqual(444, computer.PopOutput());
        }
コード例 #4
0
ファイル: IntComputer.cs プロジェクト: alexis2b/aoc2019
        public void TestReadOutOfBounds()
        {
            var program = new long[]   {
                1, 10, 11, 7, 4, 7, 99, 321
            };                                                // add[10]+[11]->[7], output [7] -> expect [7] to be zero (0+0)
            var computer = new IntComputer();

            computer.Run(program);
            Assert.AreEqual(0, computer.PopOutput());
        }
コード例 #5
0
        public static long RunSensorBoost(IEnumerable <long> program)
        {
            var computer = new IntComputer();

            computer.Run(program, new long[] { 2 }); // 2 is for Actual Boost Mode
            if (computer.OutputCount > 1)
            {
                throw new Exception("there should be only one output");
            }
            return(computer.PopOutput());
        }
コード例 #6
0
        public static long RunBoostDiagnostics(IEnumerable <long> program)
        {
            var computer = new IntComputer();

            computer.Run(program, new long[] { 1 }); // 1 is for Test Mode
            if (computer.OutputCount > 1)
            {
                throw new Exception("self-diagnostic failed, error: " + string.Join(",", computer.Outputs));
            }
            return(computer.PopOutput());
        }
コード例 #7
0
ファイル: IntComputer.cs プロジェクト: alexis2b/aoc2019
        public void TestRequestInputAndContinue()
        {
            var program = new long[]   {
                3, 5, 4, 5, 99, 0
            };
            var computer = new IntComputer();

            Assert.AreEqual(IntComputer.ExitCode.NotStarted, computer.LastExitCode);
            var ec1 = computer.Run(program);

            Assert.AreEqual(IntComputer.ExitCode.NeedInput, ec1);
            Assert.AreEqual(IntComputer.ExitCode.NeedInput, computer.LastExitCode);

            const long newInput = 321;
            var        ec2      = computer.Continue(new[] { newInput });

            Assert.AreEqual(IntComputer.ExitCode.Ended, ec2);
            Assert.AreEqual(IntComputer.ExitCode.Ended, computer.LastExitCode);
            Assert.AreEqual(newInput, computer.Memory[5]);
            Assert.AreEqual(1, computer.OutputCount);
            Assert.AreEqual(newInput, computer.PopOutput());
            Assert.AreEqual(0, computer.OutputCount);
        }
コード例 #8
0
ファイル: Day13.cs プロジェクト: alexis2b/aoc2019
        public static long GetScoreAfterBreakingAllBlocks(IEnumerable <long> program)
        {
            var modifiedProgram = program.ToList();

            modifiedProgram[0] = 2; // 2 quarters inserted
            var  computer = new IntComputer();
            long score    = 0L;
            var  bx       = 0; // ball x-position, for autoplay
            var  px       = 0; // paddle x-position, for autoplay

            computer.Run(modifiedProgram);
            var screen = Enumerable.Range(0, 21).Select(i => new StringBuilder(new String('X', 44))).ToList();

            Console.Clear();
            do
            {
                while (computer.OutputCount >= 3)
                {
                    var  x = (int)computer.PopOutput();
                    var  y = (int)computer.PopOutput();
                    long v = computer.PopOutput();
                    if (x == -1 && y == 0)
                    {
                        score = v;
                    }
                    else
                    {
                        screen[y][x] = v == 0 ? ' ' : v == 1 ? 'W' : v == 2 ? '#' : v == 3 ? '=' : v == 4 ? 'O' : '?';
                    }
                    if (v == 3 && x >= 0)
                    {
                        px = x;
                    }
                    if (v == 4 && x >= 0)
                    {
                        bx = x;
                    }
                }

                // display
//                Console.SetCursorPosition(0, 0);
//                screen.ForEach(r => Console.WriteLine(r));
//                Console.WriteLine("Score: " + score);

                //System.Threading.Thread.Sleep(150);

                // input joystick
                var joystick = Math.Sign(bx - px);

                // if ( Console.KeyAvailable )
                // switch(Console.ReadKey(true).Key)
                // {
                //     case ConsoleKey.LeftArrow:  joystick = -1; break;
                //     case ConsoleKey.RightArrow: joystick =  1; break;
                // }
                computer.AddInput(joystick);
            } while(computer.Continue(Enumerable.Empty <long>()) != IntComputer.ExitCode.Ended);

            // final score
            while (computer.OutputCount >= 3)
            {
                var  x = (int)computer.PopOutput();
                var  y = (int)computer.PopOutput();
                long v = computer.PopOutput();
                if (x == -1 && y == 0)
                {
                    score = v;
                }
            }

            return(score);
        }