Example #1
0
        private ResultOpcode ExecuteAmplifier(bool isLoopMode, string[] array, string amplifierName, int phase, List <Int64> input, Int64 state = 0)
        {
            ResultOpcode result        = new ResultOpcode();
            bool         isPhaseNumber = true;

            if (phase < 0)
            {
                Console.WriteLine("Define phase setting for amplifier " + amplifierName + ":");
                string phaseSettingString = Console.ReadLine();
                isPhaseNumber = int.TryParse(phaseSettingString, out phase);
            }
            if (isPhaseNumber && ((isLoopMode && (phase < 10) && (phase >= 5)) || (!isLoopMode && (phase < 5) && (phase >= 0))))
            {
                IntcodeProgram intcodeProgram = new IntcodeProgram();
                result = intcodeProgram.IntcodeExtended(array, input, state);

                if (amplifierName.Equals("E") && result.output > maximum)
                {
                    maximum = result.output;
                }
            }
            else
            {
                Console.WriteLine("Wrong phase setting value");
            }
            return(result);
        }
Example #2
0
 public void Day5Part2Example4()
 {
     intcodeProgram = new IntcodeProgram("3,3,1105,-1,9,1101,0,0,12,4,12,99,1");
     intcodeProgram.AddToInput(500); // 500 arbitrary value
     intcodeProgram.Execute();
     Assert.AreEqual(1, intcodeProgram.Outputs[0]);
 }
Example #3
0
 public void Day5Part2Example5(int input, int expected)
 {
     intcodeProgram = new IntcodeProgram("3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99");
     intcodeProgram.AddToInput(input);
     intcodeProgram.Execute();
     Assert.AreEqual(expected, intcodeProgram.Outputs[0]);
 }
        public void Test2()
        {
            var programs = "2,3,0,3,99";
            var result   = IntcodeProgram.ProcessProgramString(programs);

            Assert.AreEqual("2,3,0,6,99", result);
        }
Example #5
0
 public void Day5Part2Example3()
 {
     intcodeProgram = new IntcodeProgram("3,3,1105,-1,9,1101,0,0,12,4,12,99,1");
     intcodeProgram.AddToInput(0);
     intcodeProgram.Execute();
     Assert.AreEqual(0, intcodeProgram.Outputs[0]);
 }
Example #6
0
 public void Day5Part1Example1()
 {
     intcodeProgram = new IntcodeProgram("3,0,4,0,99");
     intcodeProgram.AddToInput(1);
     intcodeProgram.Execute();
     Assert.AreEqual(1, intcodeProgram.Outputs[0]);
 }
Example #7
0
 public void Day5Part2Example1()
 {
     intcodeProgram = new IntcodeProgram("3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9");
     intcodeProgram.AddToInput(0);
     intcodeProgram.Execute();
     Assert.AreEqual(0, intcodeProgram.Outputs[0]);
 }
        public void Test4()
        {
            var programs = "1,1,1,4,99,5,6,0,99";
            var result   = IntcodeProgram.ProcessProgramString(programs);

            Assert.AreEqual("30,1,1,4,2,5,6,0,99", result);
        }
Example #9
0
        public void Day2(int nounStart, int nounEnd, int verbStart, int verbEnd, int expectedOutput)
        {
            int[] intcodeProgram = File.ReadAllText("day2.txt")
                                   .Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                                   .Select(number => int.Parse(number, CultureInfo.InvariantCulture))
                                   .ToArray();

            for (int noun = nounStart; noun <= nounEnd; noun++)
            {
                for (int verb = verbStart; verb <= verbEnd; verb++)
                {
                    intcodeProgram[1] = noun;
                    intcodeProgram[2] = verb;

                    IntcodeProgram program = new IntcodeProgram(
                        intcodeProgram,
                        new DefaultInstructionFactory(),
                        new ImplicitOpcodeParser());

                    List <int> programOutput = program.Run();

                    if (programOutput[0] == expectedOutput)
                    {
                        _output.WriteLine($"noun: {noun}, verb: {verb}");
                        return;
                    }
                }
            }

            Assert.True(false, "Result not found.");
        }
Example #10
0
        public void IntroTest()
        {
            var programs = "1,9,10,3,2,3,11,0,99,30,40,50";
            var result   = IntcodeProgram.ProcessProgramString(programs);

            Assert.AreEqual("3500,9,10,70,2,3,11,0,99,30,40,50", result);
        }
Example #11
0
        public void Test3()
        {
            var programs = "2,4,4,5,99,0";
            var result   = IntcodeProgram.ProcessProgramString(programs);

            Assert.AreEqual("2,4,4,5,99,9801", result);
        }
Example #12
0
 public void Day5Part2Example2()
 {
     intcodeProgram = new IntcodeProgram("3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9");
     intcodeProgram.AddToInput(500); // 500 arbitrary value
     intcodeProgram.Execute();
     Assert.AreEqual(1, intcodeProgram.Outputs[0]);
 }
Example #13
0
        public void Execute(IntcodeProgram program)
        {
            int firstParam     = program.GetValueBasedOnParameterMode(1, firstParameterMode);
            int secondParam    = program.GetValueBasedOnParameterMode(2, secondParameterMode);
            int resultPosition = program[program.InstructionPointer + 3];

            program[resultPosition]     = firstParam * secondParam;
            program.InstructionPointer += 4;
        }
Example #14
0
        public void TestExecuteIntcode_2()
        {
            string         intCode        = "1,1,1,4,99,5,6,0,99";
            IntcodeProgram intcodeProgram = new IntcodeProgram(intCode);

            int result = intcodeProgram.ExecuteIntcode().position0;

            Assert.AreEqual(30, result);
        }
Example #15
0
        public void TestFindNounAndVerb()
        {
            string         intCode        = "1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,9,1,19,1,19,5,23,1,23,6,27,2,9,27,31,1,5,31,35,1,35,10,39,1,39,10,43,2,43,9,47,1,6,47,51,2,51,6,55,1,5,55,59,2,59,10,63,1,9,63,67,1,9,67,71,2,71,6,75,1,5,75,79,1,5,79,83,1,9,83,87,2,87,10,91,2,10,91,95,1,95,9,99,2,99,9,103,2,10,103,107,2,9,107,111,1,111,5,115,1,115,2,119,1,119,6,0,99,2,0,14,0";
            IntcodeProgram intcodeProgram = new IntcodeProgram(intCode);

            int result = intcodeProgram.FindNounAndVerb(19690720);

            Assert.AreEqual(7749, result);
        }
Example #16
0
        public void TestExecuteIntcode_1()
        {
            string         intCode        = "2,4,4,5,99,0";
            IntcodeProgram intcodeProgram = new IntcodeProgram(intCode);

            int result = intcodeProgram.ExecuteIntcode().position0;

            Assert.AreEqual(2, result);
        }
        public void TestLargerExample()
        {
            int            input          = 7;
            string         program        = "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99";
            IntcodeProgram intcodeProgram = new IntcodeProgram(program);

            int result = intcodeProgram.ExecuteIntcode(input).outputs.Last();

            Assert.AreEqual(999, result);
        }
        public void Test04()
        {
            int            input          = 1;
            string         program        = "3,3,1105,-1,9,1101,0,0,12,4,12,99,1";
            IntcodeProgram intcodeProgram = new IntcodeProgram(program);

            int result = intcodeProgram.ExecuteIntcode(input).outputs.Last();

            Assert.AreEqual(1, result);
        }
        public void Test02()
        {
            int            input          = 1;
            string         program        = "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9";
            IntcodeProgram intcodeProgram = new IntcodeProgram(program);

            int result = intcodeProgram.ExecuteIntcode(input).outputs.Last();

            Assert.AreEqual(1, result);
        }
Example #20
0
        public void ProcessTests(string intcodeProgram, int position, int expectedValue)
        {
            IntcodeProgram program = new IntcodeProgram(
                intcodeProgram,
                new DefaultInstructionFactory(),
                new ImplicitOpcodeParser());

            List <int> output = program.Run();

            Assert.Equal(expectedValue, output[position]);
        }
        public void TestPart2Solution()
        {
            int    input   = 5;
            string program = System.IO.File.ReadAllText(DataHelper.getPath("05"));

            IntcodeProgram intcodeProgram = new IntcodeProgram(program);

            int result = intcodeProgram.ExecuteIntcode(input).outputs.Last();

            Assert.AreEqual(12077198, result);
        }
Example #22
0
        public void IntcodeInterpreter_Execute_OutputMatchesExamples(string script, string expected)
        {
            //Arrange
            var program     = new IntcodeProgram(script);
            var interpreter = new IntcodeInterpreter();

            //Act
            interpreter.Execute(program);

            //Assert
            Assert.Equal(expected, program.ToString());
        }
Example #23
0
        public object Run(string input)
        {
            var program = new IntcodeProgram(input);

            program.Addresses[1] = 12;
            program.Addresses[2] = 2;

            var interpreter = new IntcodeIntepreter();
            var result      = interpreter.Run(program);

            return(result.Addresses.ElementAt(0));
        }
Example #24
0
        private void ManageID()
        {
            Console.WriteLine("Introduce the ID of the system to test:");
            string input = Console.ReadLine().Trim();

            if (input.Trim().Equals("1") || input.Trim().Equals("5"))
            {
                string[]       array          = File.ReadAllText(Directory.GetCurrentDirectory() + "\\Files\\Day5-Input.txt").Split(',');
                IntcodeProgram intcodeProgram = new IntcodeProgram();
                Int64          output         = intcodeProgram.Intcode(array, null);
                Console.WriteLine("Diagnostic code:{0}", output);
            }
        }
        public void Execute(IntcodeProgram program)
        {
            int firstVal = program.GetValueBasedOnParameterMode(1, firstParameterMode);

            if (firstVal != 0)
            {
                int secondVal = program.GetValueBasedOnParameterMode(2, secondParameterMode);
                program.InstructionPointer = secondVal;
            }
            else
            {
                program.InstructionPointer += 3;
            }
        }
        public void Execute(IntcodeProgram program)
        {
            // Due to the nature of the input operation, we cannot use the program.GetValueBasedOnParameterMode method.
            switch (parameterMode)
            {
            case ParameterMode.PositionMode:
                program[program[program.InstructionPointer + 1]] = inputParameter;
                break;

            case ParameterMode.ImmediateMode:
                program[program.InstructionPointer + 1] = inputParameter;
                break;
            }
            program.InstructionPointer += 2;
        }
        private async Task TestIntcodeProgramAsync(long[] memory, int input, int expectedOutput)
        {
            // Arrange
            var program = new IntcodeProgram(NullLogger <IntcodeProgram> .Instance);

            program.Init(memory);
            var output = Channel.CreateUnbounded <long>();

            // Act
            await program.RunAsync(await input.ToChannelAsync(), output);

            // Assert
            var result = await output.Reader.ReadAsync();

            Assert.Equal(expectedOutput, result);
        }
        public async Task Part1_Example3_ShouldOutputLong2()
        {
            // Arrange
            var input   = new long[] { 104, 1125899906842624, 99 };
            var program = new IntcodeProgram(NullLogger <IntcodeProgram> .Instance);

            program.Init(input);
            var output = Channel.CreateUnbounded <long>();

            // Act
            await program.RunAsync(Channel.CreateUnbounded <long>(), output);

            // Assert
            var result = await output.Reader.ReadAsync();

            Assert.Equal(1125899906842624, result);
        }
        public async Task Part1_Example2_ShouldOutputLong()
        {
            // Arrange
            var input   = new long[] { 1102, 34915192, 34915192, 7, 4, 7, 99, 0 };
            var program = new IntcodeProgram(NullLogger <IntcodeProgram> .Instance);

            program.Init(input);
            var output = Channel.CreateUnbounded <long>();

            // Act
            await program.RunAsync(Channel.CreateUnbounded <long>(), output);

            // Assert
            var result = await output.Reader.ReadAsync();

            Assert.Equal(1219070632396864, result);
        }
        public async Task Part1_Example1_ShouldOutputItself()
        {
            // Arrange
            var input   = new long[] { 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 };
            var program = new IntcodeProgram(NullLogger <IntcodeProgram> .Instance);

            program.Init(input);
            var output = Channel.CreateUnbounded <long>();

            // Act
            await program.RunAsync(Channel.CreateUnbounded <long>(), output);

            // Assert
            var result = await output.Reader.ReadAllAsync().ToListAsync();

            Assert.Equal(input, result);
        }