public void TestLessThan8PositionMode(long input, long expected) { var computer = new IntComputer(); computer.Run(new long[] { 3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8 }, new[] { input }); Assert.AreEqual(expected, computer.Outputs[0]); }
public void JumpTestImmediateMode(long input, long expected) { var computer = new IntComputer(); computer.Run(new long[] { 3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1 }, new[] { input }); Assert.AreEqual(expected, computer.Outputs[0]); }
private static (long first, long second) GetTwoParameters(IntComputer computer, long position, long instruction) { var first = Resolve(computer, GetParameter(position, instruction, Parameter.First)); var second = Resolve(computer, GetParameter(position, instruction, Parameter.Second)); return(first, second); }
public void Test1(long[] program, int position, long value) { var computer = new IntComputer(); computer.Run(program); Assert.AreEqual(value, computer.Memory[position]); }
public void JumpTestPositionMode(long input, long expected) { var computer = new IntComputer(); computer.Run(new long[] { 3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9 }, new[] { input }); Assert.AreEqual(expected, computer.Outputs[0]); }
public void TestLessThan8ImmediateMode(long input, long expected) { var computer = new IntComputer(); computer.Run(new long[] { 3, 3, 1107, -1, 8, 3, 4, 3, 99 }, new[] { input }); Assert.AreEqual(expected, computer.Outputs[0]); }
private static long AdjustRelativeBase(long position, long instruction, IntComputer computer) { var first = Resolve(computer, GetParameter(position, instruction, Parameter.First)); computer.RelativeBase += first; return(position + 2); }
private static long Output(long position, long instruction, IntComputer computer) { var first = ResolveAddress(computer, GetParameter(position, instruction, Parameter.First)); computer._output.Enqueue(computer.Memory[(int)first]); return(position + 2); }
public void Test1_3() { var program = new long[] { 104, 1125899906842624, 99 }; var computer = new IntComputer(); computer.Run(program, new long[] {}); Assert.AreEqual(1125899906842624L, computer.PopOutput()); }
public void Test1_1() { var program = new long[] { 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 }; var computer = new IntComputer(); computer.Run(program, new long[] {}); Assert.AreEqual(program, computer.Outputs.ToArray()); }
private static (long first, long second, long third) GetThreeParameters(IntComputer computer, long position, long instruction) { var first = Resolve(computer, GetParameter(position, instruction, Parameter.First)); var second = Resolve(computer, GetParameter(position, instruction, Parameter.Second)); var third = ResolveAddress(computer, GetParameter(position, instruction, Parameter.Third)); return(first, second, third); }
public void Test1() { var input = 54321; var computer = new IntComputer(); computer.Run(new long[] { 3, 0, 4, 0, 99 }, new long[] { input }); Assert.AreEqual(input, computer.Outputs[0]); }
private static long EqualsOp(long position, long instruction, IntComputer computer) { var(first, second, third) = GetThreeParameters(computer, position, instruction); computer.Memory[(int)third] = second == first ? 1 : 0; return(position + 4); }
private static long Multiplication(long position, long instruction, IntComputer computer) { var(first, second, third) = GetThreeParameters(computer, position, instruction); computer.Memory[(int)third] = second * first; return(position + 4); }
public void TestComparisonTo8(long input, long expected) { var computer = new IntComputer(); computer.Run(new long[] { 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 }, new[] { input }); Assert.AreEqual(expected, computer.Outputs[0]); }
public void Test1_2() { var program = new long[] { 1102, 34915192, 34915192, 7, 4, 7, 99, 0 }; var computer = new IntComputer(); computer.Run(program, new long[] {}); var outputStr = computer.PopOutput().ToString(CultureInfo.InvariantCulture); Assert.AreEqual(16, outputStr.Length); }
public void TestReadOutOfBounds() { var program = new long[] { 1, 10, 11, 7, 4, 7, 99, 321 }; // add[10]+[11]->[7], output [7] -> expect [7] to be zero (0+0) var computer = new IntComputer(); computer.Run(program); Assert.AreEqual(0, computer.PopOutput()); }
public void TestWriteOutOfBounds() { var program = new long[] { 1, 7, 8, 1024, 4, 1024, 99, 321, 123 }; // add[7]+[8]->[1024], output [1024] -> expect [1024] to be 444 (321+123) var computer = new IntComputer(); computer.Run(program); Assert.AreEqual(444, computer.PopOutput()); }
public static long RunBoostDiagnostics(IEnumerable <long> program) { var computer = new IntComputer(); computer.Run(program, new long[] { 1 }); // 1 is for Test Mode if (computer.OutputCount > 1) { throw new Exception("self-diagnostic failed, error: " + string.Join(",", computer.Outputs)); } return(computer.PopOutput()); }
public static long RunSensorBoost(IEnumerable <long> program) { var computer = new IntComputer(); computer.Run(program, new long[] { 2 }); // 2 is for Actual Boost Mode if (computer.OutputCount > 1) { throw new Exception("there should be only one output"); } return(computer.PopOutput()); }
public static long ExecuteAndGetPosition0(IEnumerable <long> input) { var modifiedInput = input.ToList(); modifiedInput[1] = 12; modifiedInput[2] = 2; var computer = new IntComputer(); computer.Run(modifiedInput); return(computer.Memory[0]); }
public static long RunCode(IEnumerable <long> program, string code) { // execute script and get results var computer = new IntComputer(); var ec = computer.Run(program, code.Select(c => (long)c)); Console.WriteLine("Exit code: " + ec); Console.WriteLine("Outputs: " + String.Join(',', computer.Outputs)); Render(computer.Outputs); return(computer.Outputs.Last()); }
public static long ThrusterSignal(List <long> program, List <long> phases) { var signal = 0L; for (var i = 0; i < phases.Count; i++) { var phase = phases[i]; var computer = new IntComputer(); computer.Run(program, new[] { phase, signal }); signal = computer.Outputs[0]; } return(signal); }
private static long Input(long position, long instruction, IntComputer computer) { var first = ResolveAddress(computer, GetParameter(position, instruction, Parameter.First)); do { if (computer._input.TryDequeue(out var value)) { computer.Memory[(int)first] = value; return(position + 2); } } while (true); }
public static long CheckTestsAndGetDiagnosticCode(List <long> program, long input) { var computer = new IntComputer(); computer.Run(program, new[] { input }); var outputs = computer.Outputs; for (var i = 0; i < outputs.Count - 1; i++) { if (outputs[i] != 0) { throw new Exception($"test {i} failed"); } } return(outputs.Last()); }
public static int CountBlockTiles(IEnumerable <long> program) { var computer = new IntComputer(); computer.Run(program); var blockTilesCount = 0; for (var i = 2; i < computer.OutputCount; i += 3) { if (computer.Outputs[i] == 2 /*Block*/) { blockTilesCount++; } } return(blockTilesCount); }
public static long Part1(IEnumerable <long> program) { // create 50 computers instances, all loaded up const int nc = 50; var computers = new IntComputer[nc]; for (var i = 0; i < nc; i++) { computers[i] = new IntComputer(); computers[i].Run(program, new[] { (long)i }); } // Run until a packet is sent to interface address 255 while (true) { for (var i = 0; i < nc; i++) { // feed an input to computer [i], at least -1 if (computers[i].InputCount == 0) { computers[i].AddInput(-1L); } // continue computers[i].Continue(new long[] {}); // get the outputs of the computer [i] while (computers[i].OutputCount > 0) { var destinationAddress = (int)computers[i].PopOutput(); var x = computers[i].PopOutput(); var y = computers[i].PopOutput(); if (destinationAddress == 255) { return(y); // Part 1 solution } // push the packet to its destination Debug.Assert(destinationAddress < nc); computers[destinationAddress].AddInput(x); computers[destinationAddress].AddInput(y); } } } }
// In step1 we (manually) found the instructions // Step2 applies the program and gets the result public static long Part2_Step2(IEnumerable <long> program) { var instructions = "A,B,B,C,C,A,A,B,B,C\n" + "L,12,R,4,R,4\n" + "R,12,R,4,L,12\n" + "R,12,R,4,L,6,L,8,L,8\n" + "n\n"; // execute and get results var modifiedProgram = program.ToList(); modifiedProgram[0] = 2; var computer = new IntComputer(); var ec = computer.Run(modifiedProgram, instructions.Select(c => (long)c)); Console.WriteLine("Exit code: " + ec); Console.WriteLine("Outputs: " + String.Join(',', computer.Outputs)); return(computer.Outputs.Last()); }
private static List <string> GetMap(IEnumerable <long> program) { // execute the program var computer = new IntComputer(); var ec = computer.Run(program); var outputs = computer.Outputs.ToList(); Console.WriteLine("Exit code: " + ec); // map to a list of strings var sb = new StringBuilder(); foreach (var output in outputs) { sb.Append((char)output); } var map = sb.ToString().Split('\n', StringSplitOptions.RemoveEmptyEntries).ToList(); return(map); }
public static (int, int) GuessNounAndVerb(IEnumerable <long> input, long target) { for (var noun = 0; noun < 100; noun++) { for (var verb = 0; verb < 100; verb++) { var modifiedInput = input.ToList(); modifiedInput[1] = noun; modifiedInput[2] = verb; var computer = new IntComputer(); try { computer.Run(modifiedInput); if (computer.Memory[0] == target) { return(noun, verb); } } catch (Exception) { continue; } } } return(0, 0); }