Beispiel #1
0
    private static Dictionary <Point, LocationType> ExploreMap(string program)
    {
        var computer = new IntcodeComputer(program);
        var output   = -1;
        var current  = new Point(0, 0);
        var path     = new Stack <Point>(new List <Point> {
            current
        });
        var visited = new HashSet <Point>(new List <Point> {
            current
        });
        var map = new Dictionary <Point, LocationType>();

        map.Add(new Point(0, 0), LocationType.EMPTY);
        while (path.Count > 0)
        {
            var nextMoves = new Stack <Point>(MOVES.FindAll(move => !visited.Contains(current + move)));
            while (nextMoves.Count > 0)
            {
                var nextMove = nextMoves.Pop();
                output = (int)computer.RunProgram(new List <long> {
                    MOVES.IndexOf(nextMove) + 1
                })[0];
                var nextPosition = current + nextMove;
                visited.Add(nextPosition);
                switch (output)
                {
                case 0: map.Add(nextPosition, LocationType.WALL); break;

                case 1: map.Add(nextPosition, LocationType.EMPTY); break;

                case 2: map.Add(nextPosition, LocationType.OXYGEN); break;
                }
                if (output != 0)
                {
                    path.Push(nextPosition);
                    current = nextPosition;
                    break;
                }
            }
            if (output == 0 && path.Count > 0)
            {
                path.Pop();
                if (path.Count > 0)
                {
                    var new_current = path.Peek();
                    var move        = MOVES.IndexOf(new_current - current) + 1;
                    computer.RunProgram(new List <long> {
                        move
                    });
                    current = new_current;
                }
            }
        }
        return(map);
    }
Beispiel #2
0
        public void RunProgramComparisonTests()
        {
            // https://adventofcode.com/2019/day/5
            // 3,9,8,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
            // 3,9,7,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is less than 8; output 1(if it is) or 0(if it is not).
            // 3,3,1108,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is equal to 8; output 1(if it is) or 0(if it is not).
            // 3,3,1107,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is less than 8; output 1(if it is) or 0(if it is not).
            var testData = new List <Tuple <int[], int, int> >(new Tuple <int[], int, int>[] {
                new Tuple <int[], int, int>(new int[] { 3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8 }, 7, 0),
                new Tuple <int[], int, int>(new int[] { 3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8 }, 8, 1),
                new Tuple <int[], int, int>(new int[] { 3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8 }, 9, 0),
                new Tuple <int[], int, int>(new int[] { 3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8 }, 7, 1),
                new Tuple <int[], int, int>(new int[] { 3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8 }, 8, 0),
                new Tuple <int[], int, int>(new int[] { 3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8 }, 9, 0),
                new Tuple <int[], int, int>(new int[] { 3, 3, 1108, -1, 8, 3, 4, 3, 99 }, 7, 0),
                new Tuple <int[], int, int>(new int[] { 3, 3, 1108, -1, 8, 3, 4, 3, 99 }, 8, 1),
                new Tuple <int[], int, int>(new int[] { 3, 3, 1108, -1, 8, 3, 4, 3, 99 }, 9, 0),
                new Tuple <int[], int, int>(new int[] { 3, 3, 1107, -1, 8, 3, 4, 3, 99 }, 7, 1),
                new Tuple <int[], int, int>(new int[] { 3, 3, 1107, -1, 8, 3, 4, 3, 99 }, 8, 0),
                new Tuple <int[], int, int>(new int[] { 3, 3, 1107, -1, 8, 3, 4, 3, 99 }, 9, 0),
            });

            foreach (var testExample in testData)
            {
                var inputProvider  = new StaticValueInputProvider(testExample.Item2);
                var outputListener = new ListOutputListener();
                var computer       = new IntcodeComputer(inputProvider, outputListener);
                computer.LoadProgram(testExample.Item1);
                var status = computer.RunProgram();
                Assert.Equal(IntcodeProgramStatus.Completed, status);
                Assert.True(outputListener.Values.Count > 0);
                Assert.Equal(testExample.Item3, outputListener.Values[outputListener.Values.Count - 1]);
            }
        }
Beispiel #3
0
        public void RunProgramTest()
        {
            // Test examples taken from https://adventofcode.com/2019/day/2
            // Here are the initial and final states of a few more small programs:
            // 1,0,0,0,99 becomes 2,0,0,0,99 (1 + 1 = 2).
            // 2,3,0,3,99 becomes 2,3,0,6,99 (3 * 2 = 6).
            // 2,4,4,5,99,0 becomes 2,4,4,5,99,9801 (99 * 99 = 9801).
            // 1,1,1,4,99,5,6,0,99 becomes 30,1,1,4,2,5,6,0,99.
            var testData = new List <Tuple <int[], int[]> >(new Tuple <int[], int[]>[] {
                new Tuple <int[], int[]>(new int[] { 1, 0, 0, 0, 99 }, new int[] { 2, 0, 0, 0, 99 }),
                new Tuple <int[], int[]>(new int[] { 2, 3, 0, 3, 99 }, new int[] { 2, 3, 0, 6, 99 }),
                new Tuple <int[], int[]>(new int[] { 2, 4, 4, 5, 99, 0 }, new int[] { 2, 4, 4, 5, 99, 9801 }),
                new Tuple <int[], int[]>(new int[] { 1, 1, 1, 4, 99, 5, 6, 0, 99 }, new int[] { 30, 1, 1, 4, 2, 5, 6, 0, 99 }),
            });

            foreach (var testExample in testData)
            {
                var computer = new IntcodeComputer();
                computer.LoadProgram(testExample.Item1);
                var programStatus = computer.RunProgram();
                var result        = computer.GetProgramCopy();
                Assert.Equal(IntcodeProgramStatus.Completed, programStatus);
                Assert.Equal(testExample.Item2.Select(d => (BigInteger)d), result);
            }
        }
Beispiel #4
0
        public void RunProgramJumpTests()
        {
            // https://adventofcode.com/2019/day/5
            // Here are some jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero:
            // 3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9(using position mode)
            // 3,3,1105,-1,9,1101,0,0,12,4,12,99,1(using immediate mode)
            // The following example program uses an input instruction to ask
            // for a single number. The program will then output 999 if the
            // input value is below 8, output 1000 if the input value is equal
            // to 8, or output 1001 if the input value is greater than 8.
            // 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 testData = new List <Tuple <int[], int, int> >(new Tuple <int[], int, int>[] {
                new Tuple <int[], int, int>(new int[] { 3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9 }, -1, 1),
                new Tuple <int[], int, int>(new int[] { 3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9 }, 0, 0),
                new Tuple <int[], int, int>(new int[] { 3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9 }, 1, 1),
                new Tuple <int[], int, int>(new int[] { 3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1 }, -1, 1),
                new Tuple <int[], int, int>(new int[] { 3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1 }, 0, 0),
                new Tuple <int[], int, int>(new int[] { 3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1 }, 1, 1),
                new Tuple <int[], int, int>(new int[] { 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 }, 7, 999),
                new Tuple <int[], int, int>(new int[] { 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 }, 8, 1000),
                new Tuple <int[], int, int>(new int[] { 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 }, 9, 1001)
            });

            foreach (var testExample in testData)
            {
                var inputProvider  = new StaticValueInputProvider(testExample.Item2);
                var outputListener = new ListOutputListener();
                var computer       = new IntcodeComputer(inputProvider, outputListener);
                computer.LoadProgram(testExample.Item1);
                var status = computer.RunProgram();
                Assert.Equal(IntcodeProgramStatus.Completed, status);
                Assert.True(outputListener.Values.Count > 0);
                Assert.Equal(testExample.Item3, outputListener.Values[outputListener.Values.Count - 1]);
            }
        }
Beispiel #5
0
        public static BigInteger RunVaccuumRobot(bool isManualInput)
        {
            // Upon inspection, the following commands solve the problem:
            // A,B,A,B,A,C,B,C,A,C
            // A: L,6,R,6,6,L,6
            // B: R,6,6,L,5,5,L,4,L,6
            // C: L,5,5,L,5,5,L,4,L,6
            BigInteger[] program = GetDay17Input();
            program[0] = 2;
            var encodedCommands = EncodeRobotCommands(
                mainMovementRoutine: "A,B,A,B,A,C,B,C,A,C",
                movementFunctionA: "L,6,R,6,6,L,6",
                movementFunctionB: "R,6,6,L,5,5,L,4,L,6",
                movementFunctionC: "L,5,5,L,5,5,L,4,L,6",
                continuousVideoFeed: false);
            IInputProvider inputProvider;

            if (isManualInput)
            {
                inputProvider = new ConsoleInputProvider();
            }
            else
            {
                inputProvider = new BufferedInputProvider();
            }
            var             outputListener = new ListOutputListener();
            IntcodeComputer computer       = new IntcodeComputer(inputProvider, outputListener);

            computer.LoadProgram(program);
            var computerStatus   = IntcodeProgramStatus.Running;
            int outputStartIndex = 0;
            int commandIndex     = 0;

            while (IntcodeProgramStatus.Running.Equals(computerStatus) ||
                   IntcodeProgramStatus.AwaitingInput.Equals(computerStatus))
            {
                // Provide inputs if automated
                if (IntcodeProgramStatus.AwaitingInput.Equals(computerStatus) &&
                    !isManualInput)
                {
                    ((BufferedInputProvider)inputProvider).AddInputValue(encodedCommands[commandIndex]);
                    Console.Write(encodedCommands[commandIndex]);
                    commandIndex++;
                }

                // Run program
                computerStatus = computer.RunProgram();

                // Display output
                if (outputListener.Values.Count > 0)
                {
                    DisplayProgramOutput(outputListener, outputStartIndex);
                    outputStartIndex = outputListener.Values.Count;
                }
            }
            return(outputListener.Values.LastOrDefault());
        }
Beispiel #6
0
        public static BigInteger GetDay21Part1Answer()
        {
            // Program the springdroid with logic that allows it to survey the
            // hull without falling into space. What amount of hull damage
            // does it report?
            // Answer: 19360724
            BigInteger result = 0;

            Console.WriteLine("Starting Day 21 - Part 1...");
            var program        = GetDay21Input();
            var inputProvider  = new BufferedInputProvider();
            var outputListener = new ListOutputListener();
            var computer       = new IntcodeComputer(inputProvider, outputListener);

            computer.LoadProgram(program);

            // Define the robot instructions
            // Note - a jump will land 4 spaces ahead
            // Jump only if any of A, B, or C are holes
            // AND D is not a hole
            // ->
            // NOT A J <- JUMP is true if A is a hole
            // NOT B T <- TEMP is true if B is a hole
            // OR T J  <- JUMP is true if A or B is a hole
            // NOT C T <- TEMP is true if C is a hole
            // OR T J  <- JUMP is true if A or B or C is a hole
            // NOT D T <- TEMP is true if D is a hole
            // NOT T T <- TEMP is true if D is *not* a hole
            // AND T J <- JUMP is true if (A or B or C is a hole) AND (D is not a hole)
            var robotInstructions = new List <string>()
            {
                "NOT A J",
                "NOT B T",
                "OR T J",
                "NOT C T",
                "OR T J",
                "NOT D T",
                "NOT T T",
                "AND T J",
                "WALK"
            };
            var robotInstructionAsciiInput = GetRobotInstructionAsciiInputValues(robotInstructions);

            inputProvider.Values.AddRange(robotInstructionAsciiInput);

            computer.RunProgram();
            // If the robot successfully made it across, then the last output
            // value will be a large non-ascii character
            if (outputListener.Values.Count > 0 && outputListener.Values.Last() > 255)
            {
                result = outputListener.Values.Last();
                outputListener.Values.RemoveAt(outputListener.Values.Count - 1);
            }
            IntcodeComputerHelper.DrawAsciiOutput(outputListener.Values);
            return(result);
        }
        public void Operation1_PositionMode_AddsValues()
        {
            string          program  = "1,0,0,0,99";
            IntcodeComputer computer = new IntcodeComputer();

            computer.AddProgram("key", program);
            computer.RunProgram("key");

            Assert.Equal(2, computer.GetProgramAddressValue("key", 0));
        }
        public void Operation2_PositionMode_MultipliesValues()
        {
            string          program  = "2,3,0,3,99";
            IntcodeComputer computer = new IntcodeComputer();

            computer.AddProgram("key", program);
            computer.RunProgram("key");

            Assert.Equal(6, computer.GetProgramAddressValue("key", 3));
        }
Beispiel #9
0
    private static int Solve1(string program)
    {
        var computer = new IntcodeComputer(program);
        var output   = -1;
        var current  = new Point(0, 0);
        var path     = new Stack <Point>(new List <Point> {
            current
        });
        var visited = new HashSet <Point>(new List <Point> {
            current
        });

        while (output != 2)
        {
            var nextMoves = new Stack <Point>(MOVES.FindAll(move => !visited.Contains(current + move)));
            while (nextMoves.Count > 0)
            {
                var nextMove = nextMoves.Pop();
                output = (int)computer.RunProgram(new List <long> {
                    MOVES.IndexOf(nextMove) + 1
                })[0];
                var nextPosition = current + nextMove;
                visited.Add(nextPosition);
                if (output != 0)
                {
                    path.Push(nextPosition);
                    current = nextPosition;
                    break;
                }
            }
            if (output == 0)
            {
                path.Pop();
                var new_current = path.Peek();
                var move        = MOVES.IndexOf(new_current - current) + 1;
                computer.RunProgram(new List <long> {
                    move
                });
                current = new_current;
            }
        }
        return(path.Count - 1);
    }
        public void Operation1_ImmediateMode_AddsValues()
        {
            // First param is in immediate mode so its value is 0
            string          program  = "101,0,0,0,99";
            IntcodeComputer computer = new IntcodeComputer();

            computer.AddProgram("key", program);
            computer.RunProgram("key");

            Assert.Equal(101, computer.GetProgramAddressValue("key", 0));
        }
        public void Operation2_ImmediateMode_MultipliesValues()
        {
            // First param is in immediate mode so its value is 0
            string          program  = "1002,4,3,4,33";
            IntcodeComputer computer = new IntcodeComputer();

            computer.AddProgram("key", program);
            computer.RunProgram("key");

            Assert.Equal(99, computer.GetProgramAddressValue("key", 4));
        }
Beispiel #12
0
            internal static int Part1(int[] memory)
            {
                memory[1] = 12;
                memory[2] = 2;

                var computer = new IntcodeComputer(memory);

                computer.RunProgram();

                return(memory[0]);
            }
Beispiel #13
0
        public static ConsoleOutputListener RunDay5Part2()
        {
            // Input 5, output final value
            // Answer: Diagnostic value output: 7704130
            var program        = GetDay5Input();
            var outputListener = new ConsoleOutputListener();
            var computer       = new IntcodeComputer(new StaticValueInputProvider(5), outputListener);

            computer.LoadProgram(program);
            computer.RunProgram();
            return(outputListener);
        }
        public void Operation4_ImmediateMode_TakesValue_OutputsValue()
        {
            string          program  = "104,0,99";
            IntcodeComputer computer = new IntcodeComputer();

            ProgramIO programIO = new ProgramIO();

            computer.AddProgram("key", program, programIO);
            computer.RunProgram("key");

            Assert.Equal("0", programIO.ReadLineOutput());
        }
        public void Operation6_ImmediateMode_ValueTrue_DoesntJumpFirstOutput()
        {
            string          program  = "1106,0,6,4,1,99,4,0,99";
            IntcodeComputer computer = new IntcodeComputer();

            ProgramIO programIO = new ProgramIO();

            computer.AddProgram("key", program, programIO);
            computer.RunProgram("key");

            Assert.Equal("1106", programIO.ReadLineOutput());
        }
        public void Operation6_ImmediateMode_ValueFalse_JumpsToSecondOutput()
        {
            string          program  = "1106,1,6,4,1,99,4,0,99";
            IntcodeComputer computer = new IntcodeComputer();

            ProgramIO programIO = new ProgramIO();

            computer.AddProgram("key", program, programIO);
            computer.RunProgram("key");

            Assert.Equal("1", programIO.ReadLineOutput());
        }
        public void Operation6_PositionMode_ValueTrue_DoesntJumpFirstOutput()
        {
            string          program  = "6,3,4,0,4,2,99";
            IntcodeComputer computer = new IntcodeComputer();

            ProgramIO programIO = new ProgramIO();

            computer.AddProgram("key", program, programIO);
            computer.RunProgram("key");

            Assert.Equal("4", programIO.ReadLineOutput());
        }
        public void Operation5_PositionMode_ValueTrue_JumpsToSecondOutput()
        {
            string          program  = "5,1,3,7,4,1,99,4,0,99";
            IntcodeComputer computer = new IntcodeComputer();

            ProgramIO programIO = new ProgramIO();

            computer.AddProgram("key", program, programIO);
            computer.RunProgram("key");

            Assert.Equal("5", programIO.ReadLineOutput());
        }
Beispiel #19
0
        public void Should_Calculate_Correctly_Part_One(string program, string phaseSetting, int expected)
        {
            var result = 0;

            for (var i = 0; i < 5; i++)
            {
                var computer = new IntcodeComputer(program);
                computer.RunProgram(int.Parse(phaseSetting[i].ToString()), result);
                result = computer._output[0];
            }

            Assert.Equal(expected, result);
        }
Beispiel #20
0
        public static Dictionary <GridPoint, string> PerformInitialScan()
        {
            BigInteger[]    program        = GetDay17Input();
            var             inputProvider  = new BufferedInputProvider();
            var             outputListener = new ListOutputListener();
            IntcodeComputer computer       = new IntcodeComputer(inputProvider, outputListener);

            computer.LoadProgram(program);
            computer.RunProgram();
            var scaffoldMap = ProcessInitialScan(outputListener.Values);

            return(scaffoldMap);
        }
Beispiel #21
0
        public static DroneStatus ScanPoint(BigInteger[] program, GridPoint point)
        {
            var inputProvider  = new BufferedInputProvider();
            var outputListener = new ListOutputListener();
            var computer       = new IntcodeComputer(inputProvider, outputListener);

            computer.LoadProgram(program);
            inputProvider.AddInputValue(point.X);
            inputProvider.AddInputValue(point.Y);
            computer.RunProgram();
            var droneStatus = (DroneStatus)(int)outputListener.Values[0];

            return(droneStatus);
        }
        public void Operation3_PositionMode_TakesInput_SetsValue()
        {
            string          program  = "3,0,99";
            IntcodeComputer computer = new IntcodeComputer();

            //StringReader reader = new StringReader("1");

            ProgramIO programIO = new ProgramIO();

            programIO.WriteLineInput("1");

            computer.AddProgram("key", program, programIO);
            computer.RunProgram("key");

            Assert.Equal(1, computer.GetProgramAddressValue("key", 0));
        }
        public void Operation3_RelativeMode()
        {
            string code       = "109,-1,203,6,104,1,99";
            string programKey = "Day9";

            IntcodeComputer computer  = new IntcodeComputer();
            ProgramIO       programIO = new ProgramIO();

            programIO.WriteLineInput("2");

            computer.AddProgram(programKey, code, programIO);

            computer.RunProgram(programKey);

            Assert.Equal("2", programIO.ReadLineOutput());
        }
Beispiel #24
0
        public void RunGame()
        {
            int preProgrammedInputsIndex = 0;

            while (IntcodeProgramStatus.Running.Equals(GameStatus) ||
                   IntcodeProgramStatus.AwaitingInput.Equals(GameStatus))
            {
                OutputListener.Values.Clear();

                if (preProgrammedInputsIndex < PreProgrammedInputs.Count)
                {
                    InputProvider.AddInputValue(PreProgrammedInputs[preProgrammedInputsIndex]);
                    preProgrammedInputsIndex++;
                }
                if (!InputProvider.HasInput() &&
                    IntcodeProgramStatus.AwaitingInput.Equals(GameStatus))
                {
                    int paddleCommand = GetPaddleCommand();
                    InputProvider.AddInputValue(paddleCommand);
                }
                GameStatus = _computer.RunProgram();

                // Output values must be a multiple of 3
                if (OutputListener.Values.Count % 3 != 0)
                {
                    throw new Exception("Invalid output count encountered");
                }

                RefreshGridCells();
                if (DrawBoard)
                {
                    DrawGameBoard();
                }
                if (GameMode.Manual.Equals(Mode) &&
                    IntcodeProgramStatus.AwaitingInput.Equals(GameStatus))
                {
                    PauseGameAndCheckForInput();
                }
                else if (DrawBoard)
                {
                    Thread.Sleep(_automatedModeTheradSleepTimerMs);
                }

                _timeIndex++;
            }
        }
Beispiel #25
0
        public static int GetDay2Part2Answer()
        {
            // The inputs should still be provided to the program by replacing
            // the values at addresses 1 and 2, just like before. In this program,
            // the value placed in address 1 is called the noun, and the value
            // placed in address 2 is called the verb. Each of the two input
            // values will be between 0 and 99, inclusive.

            // Once the program has halted, its output is available at address 0,
            // also just like before. Each time you try a pair of inputs, make
            // sure you first reset the computer's memory to the values in the
            // program (your puzzle input) - in other words, don't reuse memory
            // from a previous attempt.
            //
            // Find the input noun and verb that cause the program to produce
            // the output 19690720. What is 100 * noun + verb?
            // (For example, if noun=12 and verb=2, the answer would be 1202.)
            // Answer: 2254
            var  initialProgram = GetDay2Input();
            int  verb           = 0;
            bool foundResult    = false;
            int  noun;

            for (noun = 0; noun <= 99; noun++)
            {
                for (verb = 0; verb <= 99; verb++)
                {
                    initialProgram[1] = noun;
                    initialProgram[2] = verb;
                    var computer = new IntcodeComputer();
                    computer.LoadProgram(initialProgram);
                    computer.RunProgram();
                    var result = computer.GetProgramCopy();
                    if (result[0] == 19690720)
                    {
                        foundResult = true;
                        break;
                    }
                }
                if (foundResult)
                {
                    break;
                }
            }
            return((100 * noun) + verb);
        }
Beispiel #26
0
            internal static int?Part2(int[] memory, int targetOutput)
            {
                for (int verb = 0; verb < 100; verb++)
                {
                    for (int noun = 0; noun < 100; noun++)
                    {
                        int[] localMemory = memory.ToArray();
                        localMemory[1] = noun;
                        localMemory[2] = verb;
                        var computer = new IntcodeComputer(localMemory);
                        if (computer.RunProgram()[0] == targetOutput)
                        {
                            return(100 * noun + verb);
                        }
                    }
                }

                return(null);
            }
Beispiel #27
0
        public static BigInteger GetDay9Part2Answer()
        {
            // The program runs in sensor boost mode by providing the input
            // instruction the value 2. Once run, it will boost the sensors
            // automatically, but it might take a few seconds to complete the
            // operation on slower hardware. In sensor boost mode, the program
            // will output a single value: the coordinates of the distress signal.
            // Run the BOOST program in sensor boost mode.What are the
            // coordinates of the distress signal?
            // Answer: 63441
            var program        = GetDay9Input();
            var inputProvider  = new StaticValueInputProvider(2);
            var outputListener = new ListOutputListener();
            var computer       = new IntcodeComputer(inputProvider, outputListener);

            computer.LoadProgram(program);
            computer.RunProgram();
            return(outputListener.Values[0]);
        }
Beispiel #28
0
        public static BigInteger GetDay2Part1Answer()
        {
            // Once you have a working computer, the first step is to restore
            // the gravity assist program (your puzzle input) to the "1202
            // program alarm" state it had just before the last computer caught
            // fire.
            // To do this, before running the program, replace position 1 with
            // the value 12 and replace position 2 with the value 2.
            // What value is left at position 0 after the program halts?
            // Answer: 11590668
            var program = GetDay2Input();

            program[1] = 12;
            program[2] = 2;
            var computer = new IntcodeComputer();

            computer.LoadProgram(program);
            computer.RunProgram();
            return(computer.GetProgramCopy()[0]);
        }
Beispiel #29
0
        public static BigInteger GetDay9Part1Answer()
        {
            // The BOOST program will ask for a single input; run it in test
            // mode by providing it the value 1. It will perform a series of
            // checks on each opcode, output any opcodes (and the associated
            // parameter modes) that seem to be functioning incorrectly, and
            // finally output a BOOST keycode.
            // Once your Intcode computer is fully functional, the BOOST
            // program should report no malfunctioning opcodes when run in test
            // mode; it should only output a single value, the BOOST keycode.
            // What BOOST keycode does it produce?
            // Answer: 2662308295
            var program        = GetDay9Input();
            var inputProvider  = new StaticValueInputProvider(1);
            var outputListener = new ListOutputListener();
            var computer       = new IntcodeComputer(inputProvider, outputListener);

            computer.LoadProgram(program);
            computer.RunProgram();
            return(outputListener.Values[0]);
        }
Beispiel #30
0
        public void RunDay9Tests()
        {
            // Test cases taken from here:
            // https://adventofcode.com/2019/day/9
            // 109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99 takes no input and produces a copy of itself as output.
            var program1        = new BigInteger[] { 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 };
            var outputListener1 = new ListOutputListener();
            var computer1       = new IntcodeComputer(new ConsoleInputProvider(), outputListener1);

            computer1.LoadProgram(program1);
            var status1 = computer1.RunProgram();

            Assert.Equal(IntcodeProgramStatus.Completed, status1);
            Assert.Equal(program1, outputListener1.Values);

            // 1102,34915192,34915192,7,4,7,99,0 should output a 16 - digit number.
            var program2        = new BigInteger[] { 1102, 34915192, 34915192, 7, 4, 7, 99, 0 };
            var outputListener2 = new ListOutputListener();
            var computer2       = new IntcodeComputer(new ConsoleInputProvider(), outputListener2);

            computer2.LoadProgram(program2);
            var status2 = computer2.RunProgram();

            Assert.Equal(IntcodeProgramStatus.Completed, status2);
            Assert.Single(outputListener2.Values);
            Assert.Equal(16, outputListener2.Values[0].ToString().Length);

            // 104,1125899906842624,99 should output the large number in the middle.
            var program3        = new BigInteger[] { 104, 1125899906842624, 99 };
            var outputListener3 = new ListOutputListener();
            var computer3       = new IntcodeComputer(new ConsoleInputProvider(), outputListener3);

            computer3.LoadProgram(program3);
            var status3 = computer3.RunProgram();

            Assert.Equal(IntcodeProgramStatus.Completed, status3);
            Assert.Single(outputListener3.Values);
            Assert.Equal(BigInteger.Parse("1125899906842624"), outputListener3.Values[0]);
        }