コード例 #1
0
        // == == == == == Puzzle 2 == == == == ==
        public static string Puzzle2(string input)
        {
            var intcode = new Intcode.Interpreter(input, memorySize: 1100);
            var keycode = intcode.ExecuteProgram_InputOutput(2, maxInstructions: 1000000);

            return(keycode.ToString());
        }
コード例 #2
0
        // == == == == == Puzzle 1 == == == == ==
        public static string Puzzle1(string input)
        {
            var intcode = new Intcode.Interpreter(input, memorySize: 1050);
            var keycode = intcode.ExecuteProgram_InputOutput(1);

            return(keycode.ToString());
        }
コード例 #3
0
ファイル: Day05.cs プロジェクト: jlintvedt/AdventOfCode2019
        // == == == == == Puzzle 2 == == == == ==
        public static string Puzzle2(string input)
        {
            var intcode           = new Intcode.Interpreter(input);
            var idThermalRadiator = 5;
            var diagnosticsCode   = intcode.ExecuteProgram_InputOutput(idThermalRadiator);

            return(diagnosticsCode.ToString());
        }
コード例 #4
0
ファイル: Day05.cs プロジェクト: jlintvedt/AdventOfCode2019
        // == == == == == Puzzle 1 == == == == ==
        public static string Puzzle1(string input)
        {
            var intcode          = new Intcode.Interpreter(input);
            var idAirConditioner = 1;
            var diagnosticsCode  = intcode.ExecuteProgram_InputOutput(idAirConditioner);

            return(diagnosticsCode.ToString());
        }
コード例 #5
0
        public void Constructor_ParseInput()
        {
            // Arrange
            var program = "1,0,0,0,99";

            // Act
            var intcode = new Intcode.Interpreter(program);

            // Assert
            Assert.AreEqual(program, intcode.GenerateProgramString());
        }
コード例 #6
0
ファイル: Day07.cs プロジェクト: jlintvedt/AdventOfCode2019
        public static long ExecuteAmplifierSequence(Intcode.Interpreter intcode, IEnumerable <long> phases)
        {
            long inputOutput = 0;

            foreach (var phase in phases)
            {
                intcode.SetInput(phase);
                inputOutput = intcode.ExecuteProgram_InputOutput(inputOutput);
            }
            return(inputOutput);
        }
コード例 #7
0
        public void ExecuteInstruction_AddThenMultiply()
        {
            // Arrange
            var program = "1,1,1,4,99,5,6,0,99";
            var intcode = new Intcode.Interpreter(program);

            // Act & Assert
            intcode.ExecuteInstruction();
            Assert.AreEqual("1,1,1,4,2,5,6,0,99", intcode.GenerateProgramString());

            intcode.ExecuteInstruction();
            Assert.AreEqual("30,1,1,4,2,5,6,0,99", intcode.GenerateProgramString());
        }
コード例 #8
0
        public void ExecuteProgram_NoudVerb_AddMultiply()
        {
            // Arrange
            var program = "1,9,10,3,2,3,11,0,99,30,40,50";
            var result  = "3500,9,10,70,2,3,11,0,99,30,40,50";
            var intcode = new Intcode.Interpreter(program);

            // Act
            intcode.ExecuteProgram_NounVerb(9, 10);

            // Assert
            Assert.AreEqual(result, intcode.GenerateProgramString());
        }
コード例 #9
0
        public void ExecuteInstruction_Add_ImmediateMode()
        {
            // Arrange
            var program = "1001,4,3,4,33";
            var result  = "1001,4,3,4,36";
            var intcode = new Intcode.Interpreter(program);

            // Act
            intcode.ExecuteInstruction();

            // Assert
            Assert.AreEqual(result, intcode.GenerateProgramString());
        }
コード例 #10
0
        public void ExecuteInstruction_Add()
        {
            // Arrange
            var program = "1,0,0,0,99";
            var result  = "2,0,0,0,99";
            var intcode = new Intcode.Interpreter(program);

            // Act
            intcode.ExecuteInstruction();

            // Assert
            Assert.AreEqual(result, intcode.GenerateProgramString());
        }
コード例 #11
0
        public void ExecuteInstruction_Add_ImmediateMode_NegativeNumber()
        {
            // Arrange
            var program = "1101,100,-1,4,0";
            var result  = "1101,100,-1,4,99";
            var intcode = new Intcode.Interpreter(program);

            // Act
            intcode.ExecuteInstruction();

            // Assert
            Assert.AreEqual(result, intcode.GenerateProgramString());
        }
コード例 #12
0
        public void ExecuteInstruction_Multiply2()
        {
            // Arrange
            var program = "2,4,4,5,99,0";
            var result  = "2,4,4,5,99,9801";
            var intcode = new Intcode.Interpreter(program);

            // Act
            intcode.ExecuteInstruction();

            // Assert
            Assert.AreEqual(result, intcode.GenerateProgramString());
        }
コード例 #13
0
        public void ExecuteProgram_ComparesAndJumps()
        {
            // Arrange
            // Uses multiple instructions on both position and immediate mode.
            // Outputs: 999 (if input <8) or 1000 (if input is 8) or 1001 (if input >8)
            var 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";
            var intcode = new Intcode.Interpreter(program);

            // Act & Assert
            Assert.AreEqual(999, intcode.ExecuteProgram_InputOutput(7));
            Assert.AreEqual(1000, intcode.ExecuteProgram_InputOutput(8));
            Assert.AreEqual(1001, intcode.ExecuteProgram_InputOutput(9));
        }
コード例 #14
0
        public void ExecuteProgram_LargeNumber()
        {
            // Arrange
            // Should the large number in the middle.
            var program = "104,1125899906842624,99";
            var intcode = new Intcode.Interpreter(program);

            // Act
            intcode.ExecuteProgram();
            var output = intcode.GetLastOutput();

            // Assert
            Assert.AreEqual(1125899906842624, output);
        }
コード例 #15
0
        public void ExecuteProgram_LargeNumbersMultiplication()
        {
            // Arrange
            // Should output a 16-digit number.
            var program = "1102,34915192,34915192,7,4,7,99,0";
            var intcode = new Intcode.Interpreter(program);

            // Act
            intcode.ExecuteProgram();
            var output = intcode.GetLastOutput();

            // Assert
            Assert.AreEqual(1219070632396864, output);
        }
コード例 #16
0
        public void ExecuteProgram_RelativePositions()
        {
            // Arrange
            // Takes no input and produces a copy of itself as output.
            var program = "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99";
            var intcode = new Intcode.Interpreter(program, memorySize: 200);

            // Act
            intcode.ExecuteProgram();
            var output = intcode.GetAllOutput(",");

            // Assert
            Assert.AreEqual($"{program},", output);
        }
コード例 #17
0
        public void ExecuteInstruction_InputThenOutput()
        {
            // Arrange
            var program = "3,0,4,0,99";
            var intcode = new Intcode.Interpreter(program);
            var input   = 13;

            // Act & Assert
            intcode.SetInput(input);
            intcode.ExecuteInstruction();
            Assert.AreEqual("13,0,4,0,99", intcode.GenerateProgramString());

            intcode.ExecuteInstruction();
            Assert.AreEqual(input, intcode.GetOutput());
        }
コード例 #18
0
ファイル: Day07.cs プロジェクト: jlintvedt/AdventOfCode2019
        // == == == == == Puzzle 1 == == == == ==
        public static string Puzzle1(string input)
        {
            var  intcode       = new Intcode.Interpreter(input);
            long highestSignal = 0;

            foreach (var phaseSettings in phasePermutationsLow)
            {
                var thrusterSignal = ExecuteAmplifierSequence(intcode, phaseSettings);
                if (thrusterSignal > highestSignal)
                {
                    highestSignal = thrusterSignal;
                }
            }

            return(highestSignal.ToString());
        }
コード例 #19
0
        // == == == == == Puzzle 2 == == == == ==
        public static string Puzzle2(string input)
        {
            var intcode = new Intcode.Interpreter(input);

            for (int noun = 0; noun < 100; noun++)
            {
                for (int verb = 0; verb < 100; verb++)
                {
                    if (intcode.ExecuteProgram_NounVerb(noun, verb) == 19690720)
                    {
                        return((100 * noun + verb).ToString());
                    }
                }
            }

            throw new Exception("Could not find noun and verb that produced 19690720");
        }
コード例 #20
0
        public void ExecuteInstruction_LessThan_ImmediateMode()
        {
            // Arrange
            var islessthan = "1107,66,77,5,99,-1";
            var expected   = "1107,66,77,5,99,1";
            var intcode    = new Intcode.Interpreter(islessthan);

            // Act & Assert
            intcode.ExecuteInstruction();
            Assert.AreEqual(expected, intcode.GenerateProgramString());

            // Arrange
            var notlessthan = "1107,66,66,5,99,-1";

            expected = "1107,66,66,5,99,0";
            intcode  = new Intcode.Interpreter(notlessthan);

            // Act & Assert
            intcode.ExecuteInstruction();
            Assert.AreEqual(expected, intcode.GenerateProgramString());
        }
コード例 #21
0
        public void ExecuteProgram_LessThan()
        {
            // Arrange
            // Checks if input is less than 8. Outputs 1 (of it is) or 0( (if it is not).
            var program_pm = "3,9,7,9,10,9,4,9,99,-1,8";
            var program_im = "3,3,1107,-1,8,3,4,3,99";

            // Act & Assert
            // Position Mode
            var intcode = new Intcode.Interpreter(program_pm);

            Assert.AreEqual(1, intcode.ExecuteProgram_InputOutput(4));
            Assert.AreEqual(0, intcode.ExecuteProgram_InputOutput(8));
            Assert.AreEqual(0, intcode.ExecuteProgram_InputOutput(10));

            // Immediate Mode
            intcode = new Intcode.Interpreter(program_im);
            Assert.AreEqual(1, intcode.ExecuteProgram_InputOutput(4));
            Assert.AreEqual(0, intcode.ExecuteProgram_InputOutput(8));
            Assert.AreEqual(0, intcode.ExecuteProgram_InputOutput(10));
        }
コード例 #22
0
        public void ExecuteProgram_JumpIf()
        {
            // Arrange
            // Jump tests. Ouputs 0 (if input was 0) or (if input was non-zero)
            var program_pm = "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9";
            var program_im = "3,3,1105,-1,9,1101,0,0,12,4,12,99,1";

            // Act & Assert
            // Position Mode
            var intcode = new Intcode.Interpreter(program_pm);

            Assert.AreEqual(0, intcode.ExecuteProgram_InputOutput(0));
            Assert.AreEqual(1, intcode.ExecuteProgram_InputOutput(1));
            Assert.AreEqual(1, intcode.ExecuteProgram_InputOutput(2));

            // Immediate Mode
            intcode = new Intcode.Interpreter(program_im);
            Assert.AreEqual(0, intcode.ExecuteProgram_InputOutput(0));
            Assert.AreEqual(1, intcode.ExecuteProgram_InputOutput(1));
            Assert.AreEqual(1, intcode.ExecuteProgram_InputOutput(2));
        }
コード例 #23
0
        public void ExecuteInstruction_Equals_PositionMode()
        {
            // Arrange
            var isequal  = "8,5,6,7,99,66,66,-1";
            var expected = "8,5,6,7,99,66,66,1";
            var intcode  = new Intcode.Interpreter(isequal);

            // Act && Assert
            intcode.ExecuteInstruction();
            Assert.AreEqual(expected, intcode.GenerateProgramString());

            // Arrange
            var notequal = "8,5,6,7,99,66,77,-1";

            expected = "8,5,6,7,99,66,77,0";
            intcode  = new Intcode.Interpreter(notequal);

            // Act && Assert
            intcode.ExecuteInstruction();
            Assert.AreEqual(expected, intcode.GenerateProgramString());
        }
コード例 #24
0
ファイル: Day07.cs プロジェクト: jlintvedt/AdventOfCode2019
        // == == == == == Puzzle 2 == == == == ==
        public static string Puzzle2(string input)
        {
            var  outCh         = Channel.CreateUnbounded <long>();
            var  ampA          = new Intcode.Interpreter(input, inputChannel: outCh);
            var  ampB          = new Intcode.Interpreter(input, inputChannel: ampA.OutputChannel);
            var  ampC          = new Intcode.Interpreter(input, inputChannel: ampB.OutputChannel);
            var  ampD          = new Intcode.Interpreter(input, inputChannel: ampC.OutputChannel);
            var  ampE          = new Intcode.Interpreter(input, inputChannel: ampD.OutputChannel, outputChannel: outCh);
            long highestSignal = 0;

            foreach (var phaseSettings in phasePermutationsHigh)
            {
                // Reset and start all amps with new phase input
                var tasks = new Task[5]
                {
                    ampA.ExecuteProgram_StartAsync(phaseSettings.ElementAt(0)),
                    ampB.ExecuteProgram_StartAsync(phaseSettings.ElementAt(1)),
                    ampC.ExecuteProgram_StartAsync(phaseSettings.ElementAt(2)),
                    ampD.ExecuteProgram_StartAsync(phaseSettings.ElementAt(3)),
                    ampE.ExecuteProgram_StartAsync(phaseSettings.ElementAt(4)),
                };

                // Start feedbackloop
                ampA.SetInput(0);

                //Wait for all tasks to reach halt
                Task.WaitAll(tasks);

                // Check if result is largest
                var result = ampE.GetOutput();
                if (result > highestSignal)
                {
                    highestSignal = result;
                }
            }

            return(highestSignal.ToString());
        }
コード例 #25
0
        // == == == == == Puzzle 1 == == == == ==
        public static string Puzzle1(string input)
        {
            var intcode = new Intcode.Interpreter(input);

            return(intcode.ExecuteProgram_NounVerb(12, 2, resetMemory: false).ToString());
        }