public string SolvePart1() { // Do for all combinations of Phase var permutations = new Permutations <int>(Enumerable.Range(0, 5).ToList()); var maxOutput = int.MinValue; foreach (var permutation in permutations) { // For Amp A -> E var signal = 0; foreach (int phase in permutation) { var amplifier = new IntCodeMachine.IntCodeMachine(Input, new[] { phase, signal }); amplifier.Execute(false); signal = int.Parse(amplifier.Outputs.First()); } // Record final output if higher than previous final if (signal > maxOutput) { maxOutput = signal; } } return($"Part 1: {maxOutput}"); }
public string SolvePart1() { var computer = new IntCodeMachine.IntCodeMachine(Input, new[] { 1 }); computer.Execute(false); return($"Part 1: {computer.Outputs[^2]}");
public string SolvePart2() { var foundAnswer = false; var answer = string.Empty; for (var noun = 0; noun < Input.Length && !foundAnswer; noun++) { for (var verb = 0; verb < Input.Length && !foundAnswer; verb++) { Input[1] = noun; Input[2] = verb; var computer = new IntCodeMachine.IntCodeMachine(Input); computer.Execute(); if (computer.Memory[0] == 19690720) { foundAnswer = true; answer = $"Part 2: 100 * {noun} + {verb} = {100 * noun + verb}"; } } } return(answer); }
public string SolvePart1() { Input[1] = 12; Input[2] = 2; var computer = new IntCodeMachine.IntCodeMachine(Input); computer.Execute(); return($"Part 1: {computer.Memory[0]}"); }
public void AdditionalTests(BigInteger[] initialState, string[] expectedOutput) { // Arrange var computer = new IntCodeMachine.IntCodeMachine(initialState, new[] { 12345 }); // Act computer.Execute(false); // Assert computer.Outputs.ShouldBe(expectedOutput.ToList()); }
public void SimpleTests(BigInteger[] initialState, BigInteger[] expectedResult) { // Arrange var computer = new IntCodeMachine.IntCodeMachine(initialState); // Act computer.Execute(false); // Assert computer.Memory.ShouldBe(new IntCodeMachine.IntCodeMachine(expectedResult).Memory); }
public void RelativeBaseTests(BigInteger[] initialState, int expectedRelativeBase, string[] expectedOutput) { // Arrange var computer = new IntCodeMachine.IntCodeMachine(initialState, new int[] { }); // Act computer.Execute(false); // Assert computer.RelativeBase.ShouldBe(expectedRelativeBase); computer.Outputs.ShouldBe(expectedOutput.ToList()); }
public void JumpTests(BigInteger[] initialState, int input, BigInteger[] expectedResult, string[] expectedOutput) { // Arrange var computer = new IntCodeMachine.IntCodeMachine(initialState, new[] { input }); // Act computer.Execute(false); // Assert computer.Memory.ShouldBe(new IntCodeMachine.IntCodeMachine(expectedResult).Memory); computer.Outputs.ShouldBe(expectedOutput.ToList()); }
public ArcadeCabinet(BigInteger[] memory) { Software = new IntCodeMachine.IntCodeMachine(memory); Software.Execute(false); Instructions = Software.Outputs.Where(o => o != "Halt") .ToList() .SplitList(3) .Select(i => new Instruction(i)) .ToList(); int maxX = Instructions.MaxBy(instruction => instruction.X).First().X + 1; int maxY = Instructions.MaxBy(instruction => instruction.Y).First().Y + 1; Grid = new int[maxX, maxY]; }
public ArcadeCabinet(BigInteger[] memory) { Software = new IntCodeMachine.IntCodeMachine(memory); Software.Execute(false); Instructions = Software.Outputs.Where(o => o != "Halt") .ToList() .SplitList(3) .Select(i => new Instruction { X = int.Parse(i[0]), Y = int.Parse(i[1]), TileId = int.Parse(i[2]) }) .ToList(); int maxX = Instructions.MaxBy(instruction => instruction.X)?.X ?? 0 + 1; int maxY = Instructions.MaxBy(instruction => instruction.Y)?.Y ?? 0 + 1; Grid = new int[maxX, maxY]; }
public string SolvePart2() { var permutations = new Permutations <int>(Enumerable.Range(5, 5).ToList()); var maxOutput = int.MinValue; foreach (var permutation in permutations) { var ampA = new IntCodeMachine.IntCodeMachine(Input, new[] { permutation.ToArray()[0] }); var ampB = new IntCodeMachine.IntCodeMachine(Input, new[] { permutation.ToArray()[1] }); var ampC = new IntCodeMachine.IntCodeMachine(Input, new[] { permutation.ToArray()[2] }); var ampD = new IntCodeMachine.IntCodeMachine(Input, new[] { permutation.ToArray()[3] }); var ampE = new IntCodeMachine.IntCodeMachine(Input, new[] { permutation.ToArray()[4] }); var signal = 0; string ampEOutput; do { ampA.InputValues.Enqueue(signal); ampA.Execute(false); string ampAOutput = ampA.Outputs.Last(); signal = int.Parse(ampAOutput == "Halt" ? ampA.Outputs[^ 2] : ampAOutput);