Exemple #1
0
        public IEnumerable <long> Run()
        {
            var buffer = new Queue <long>();
            var input  = Input.Concat(LoopBuffer());

            foreach (int i in Sequence)
            {
                var test = new IntComp(
                    program: Program.ToArray(), // a copy
                    input: input.Prepend(i)
                    );

                input = test.Run(); // output is the next input
            }

            return(CopyIntoBuffer());

            IEnumerable <long> CopyIntoBuffer()
            {
                foreach (var x in input)
                {
                    buffer.Enqueue(x);
                    yield return(x);
                }
            }

            IEnumerable <long> LoopBuffer()
            {
                while (buffer.Count > 0)
                {
                    yield return(buffer.Dequeue());
                }
            }
        }
        public void Day5_PartII_Samples(long[] program, long[] input, long[] expected)
        {
            var interpreter = new IntComp(program, input);
            var actual      = interpreter.Run();

            Assert.Equal(expected, actual);
        }
        public void Day5_IO_Write_Samples()
        {
            var program     = new long[] { 4, 2, 99 };
            var interpreter = new IntComp(program, Enumerable.Empty <long>());
            var output      = interpreter.Run();

            Assert.Equal(new long[] { 99 }, output);
        }
        public void Day9_PartI_OutOfInitialMemory()
        {
            var quine       = new long[] { 1001, 1, 0, 1000, 04, 1000, 99 };
            var interpreter = new IntComp(quine, Enumerable.Empty <long>());
            var output      = interpreter.Run();

            Assert.Equal(1, output.First());
        }
        public void Day9_PartI_LargeNumber()
        {
            var quine       = new long[] { 104, 1125899906842624, 99 };
            var interpreter = new IntComp(quine, Enumerable.Empty <long>());
            var output      = interpreter.Run();

            Assert.Equal(1125899906842624, output.First());
        }
        public void Day9_PartI_16DigitNumber()
        {
            var quine       = new long[] { 1102, 34915192, 34915192, 7, 4, 7, 99, 0 };
            var interpreter = new IntComp(quine, Enumerable.Empty <long>());
            var output      = interpreter.Run().ToList();

            Assert.Equal(16, output[0].ToString().Length);
        }
        public void Day9_PartI_Quine()
        {
            var quine       = new long[] { 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 };
            var interpreter = new IntComp(quine, Enumerable.Empty <long>());
            var output      = interpreter.Run().ToList();

            Assert.Equal(quine, output);
        }
        public void Day5_Indirect_Samples(long[] initial, long[] final)
        {
            var interpreter = new IntComp(initial, Enumerable.Empty <long>());

            _ = interpreter.Run().ToList();
            var memory = interpreter.GetMemory(initial.Length);

            Assert.Equal(final, memory);
        }
        public void Day5_IO_Read_Samples()
        {
            var program     = new long[] { 3, 1, 99 };
            var interpreter = new IntComp(program, new long[] { 42 });

            _ = interpreter.Run().ToList();
            var memory = interpreter.GetMemory(program.Length);

            Assert.Equal(new long[] { 3, 42, 99 }, memory);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Part I");
            var interpreter = new IntComp(Input, new long[] { 1 });
            var output      = interpreter.Run();

            foreach (var num in output)
            {
                Console.WriteLine(num);
            }

            Console.WriteLine("Part II");
            interpreter = new IntComp(Input, new long[] { 2 });
            output      = interpreter.Run();
            foreach (var num in output)
            {
                Console.WriteLine(num);
            }
        }