public IEnumerable <int> Run() { var buffer = new Queue <int>(); var input = Input.Concat(LoopBuffer()); foreach (int i in Sequence) { var test = new IntComp( program: Program.ToArray(), // a copy input: input.Prepend(i) ); input = test.Run(); // output is the next input } return(CopyIntoBuffer()); IEnumerable <int> CopyIntoBuffer() { foreach (var x in input) { buffer.Enqueue(x); yield return(x); } } IEnumerable <int> LoopBuffer() { while (buffer.Count > 0) { yield return(buffer.Dequeue()); } } }
public void Day5_PartII_Samples(int[] program, int[] input, int[] expected) { var interpreter = new IntComp(program, input); var actual = interpreter.Run(); Assert.Equal(expected, actual); }
public void Day5_Indirect_Samples(int[] initial, int[] final) { var interpreter = new IntComp(initial, Enumerable.Empty <int>()); _ = interpreter.Run().ToList(); Assert.Equal(final, initial); }
public void Day5_IO_Write_Samples() { var program = new[] { 4, 2, 99 }; var interpreter = new IntComp(program, Enumerable.Empty <int>()); var output = interpreter.Run(); Assert.Equal(new[] { 99 }, output); }
public void Day5_IO_Read_Samples() { var program = new[] { 3, 1, 99 }; var interpreter = new IntComp(program, new[] { 42 }); _ = interpreter.Run().ToList(); Assert.Equal(new[] { 3, 42, 99 }, program); }