public void Program_with_no_exit_instruction() { var instructions = new[] { 1, 0, 0, 0 }; var computer = new IntcodeComputerBuilder().Build(); Assert.Throws <IndexOutOfRangeException>(() => computer.Run(instructions)); }
public int Less_than_ByLocation(int x, int y) { var instructions = new[] { 7, 5, 6, 7, 99, x, y }; var computer = new IntcodeComputerBuilder().Build(); computer.Run(instructions); return(computer.Memory.GetValueImmediate(7)); }
public int Jump_if_false(int[] instructions) { var output = new QueuedOutputReceiverBuilder().Build(); var computer = new IntcodeComputerBuilder().WithOutputReceiver(output).Build(); computer.Run(instructions); return(output.Dequeue()); }
public void Simple_multiplication() { var instructions = new[] { 2, 0, 0, 0, 99 }; var computer = new IntcodeComputerBuilder().Build(); computer.Run(instructions); Assert.AreEqual(4, computer.Memory.GetValueImmediate(0)); }
public void Simple_addition_using_parameter_modes() { var instructions = new[] { 1101, 100, -1, 4, 0 }; var computer = new IntcodeComputerBuilder().Build(); computer.Run(instructions); Assert.AreEqual(99, computer.Memory.GetValueImmediate(4)); }
public void Multiple_instructions() { var instructions = new[] { 1, 1, 1, 4, 99, 5, 6, 0, 99 }; var computer = new IntcodeComputerBuilder().Build(); computer.Run(instructions); Assert.AreEqual(30, computer.Memory.GetValueImmediate(0)); }
public int Equals_immediate(int x, int y) { var instructions = new[] { 1108, x, y, 7, 99 }; var computer = new IntcodeComputerBuilder().Build(); computer.Run(instructions); return(computer.Memory.GetValueImmediate(7)); }
public int Jump_tests_examples(int[] instructions, int input) { var outputReceiver = new QueuedOutputReceiverBuilder().Build(); var inputSender = new QueuedInputSenderBuilder().WithQueuedValues(input).Build(); var computer = new IntcodeComputerBuilder().WithOutputReceiver(outputReceiver).WithInputSender(inputSender).Build(); computer.Run(instructions); return(outputReceiver.Dequeue()); }
public int Takes_input_and_saves_at_correct_location(int input, int location) { var inputSender = new QueuedInputSenderBuilder().Build(); inputSender.Enqueue(input); var computer = new IntcodeComputerBuilder().WithInputSender(inputSender).Build(); computer.Run(new[] { 3, location, 99 }); // Take an input and put the value at position 0 return(computer.Memory.GetValueImmediate(location)); }
public int Returns_output_defined_by_program(int location) { IOutputReceiver outputReceiver = new QueuedOutputReceiverBuilder().Build(); var computer = new IntcodeComputerBuilder().WithOutputReceiver(outputReceiver).Build(); computer.Run(new[] { 4, location, 99, 50, 60 }); var result = outputReceiver.Dequeue(); Assert.IsTrue(outputReceiver.IsEmpty()); return(result); }
public void Puzzle5_day2_answer() { var code = File.ReadLines("C:\\Code\\github\\AdventOfCode2019\\Puzzle5\\input.txt").First(); var interpreter = new InterpreterBuilder().Build(); var instructions = interpreter.Interpret(code); var inputSender = new QueuedInputSenderBuilder().Build(); var outputReceiver = new QueuedOutputReceiverBuilder().ThatIgnoresZeros().Build(); var computer = new IntcodeComputerBuilder().WithInputSender(inputSender).WithOutputReceiver(outputReceiver).Build(); inputSender.Enqueue(5); computer.Run(instructions); Assert.AreEqual(9217546, outputReceiver.Dequeue()); }
public void Puzzle5_day1_answer() { var code = File.ReadLines(@"C:\Users\Neil\Documents\GitHub\AdventOfCode2019\Puzzle5\input.txt").First(); var interpreter = new InterpreterBuilder().Build(); var instructions = interpreter.Interpret(code); var inputSender = new QueuedInputSenderBuilder().Build(); var outputReceiver = new QueuedOutputReceiverBuilder().ThatIgnoresZeros().Build(); var computer = new IntcodeComputerBuilder().WithInputSender(inputSender).WithOutputReceiver(outputReceiver).Build(); inputSender.Enqueue(1); computer.Run(instructions); Assert.AreEqual(6761139, outputReceiver.Dequeue()); }
public void Puzzle2_day2_answer() { var code = File.ReadLines("C:\\Code\\github\\AdventOfCode2019\\Puzzle2\\input.txt").First(); var interpreter = new InterpreterBuilder().Build(); var instructions = interpreter.Interpret(code); instructions[1] = 53; instructions[2] = 98; var computer = new IntcodeComputerBuilder().Build(); computer.Run(instructions); Assert.AreEqual(19690720, computer.Memory.GetValueImmediate(0)); }
public void Puzzle2_day1_answer() { var code = File.ReadLines(".\\Puzzle2.txt").First(); var interpreter = new InterpreterBuilder().Build(); var instructions = interpreter.Interpret(code); instructions[1] = 12; instructions[2] = 2; var computer = new IntcodeComputerBuilder().Build(); computer.Run(instructions); Assert.AreEqual(4576384, computer.Memory.GetValueImmediate(0)); }
public int Outputs_whatever_was_input(int[] instructions, int input) { var inputSender = new QueuedInputSenderBuilder().Build(); inputSender.Enqueue(input); var outputReceiver = new QueuedOutputReceiverBuilder().Build(); var computer = new IntcodeComputerBuilder() .WithInputSender(inputSender) .WithOutputReceiver(outputReceiver) .Build(); computer.Run(instructions); var result = outputReceiver.Dequeue(); Assert.IsTrue(outputReceiver.IsEmpty()); return(result); }
public void Invalid_opcode_shouldthrow(int[] instructions) { var computer = new IntcodeComputerBuilder().Build(); Assert.Throws <InvalidOperationException>(() => computer.Run(instructions)); }