private static int GetThrustSignal(params int[] phaseSettingInputs) { var amplifiers = Enumerable.Range(1, 5) .Select(x => Intcode.Intcode.LoadFromFile("Day07/AmplifierControllerSoftware.txt")) .ToArray(); var phaseSettings = new PreparedInput(phaseSettingInputs); IInput input = new PreparedInput(0); IOPipe pipe = null; foreach (var amplifier in amplifiers) { var amplifierInputs = new CombinedInput(phaseSettings, input); amplifier.SetInput(amplifierInputs); pipe = new IOPipe(); amplifier.SetOutput(pipe); input = pipe; } foreach (var amplifier in amplifiers) { amplifier.Execute().Wait(); } var result = pipe.ReadInput().Result; return(result); }
private static async Task <int> GetFeedbackThrustSignal(params int[] phaseSettingInputs) { var amplifiers = phaseSettingInputs .Select(x => Intcode.Intcode.LoadFromFile("Day07/AmplifierControllerSoftware.txt")) .ToArray(); var phaseSettings = new PreparedInput(phaseSettingInputs); var pipes = phaseSettingInputs .Select(InitialisedPipe) .ToArray(); pipes[0].Output(0); var pipeIndex = 0; foreach (var amplifier in amplifiers) { amplifier.SetInput(pipes[pipeIndex]); pipeIndex += 1; if (pipeIndex >= phaseSettingInputs.Length) { pipeIndex = 0; } amplifier.SetOutput(pipes[pipeIndex]); } var tasks = amplifiers.Select(a => a.Execute()); await Task.WhenAll(tasks); var result = await pipes[0].ReadInput(); return(result); }