private static int GetMaximumThrustValue(List <List <int> > phaseSettings, string intcodeSource) { //< Process each setting and get the result, retain all results var resultMap = new Dictionary <List <int>, int>(); foreach (var phaseSetting in phaseSettings) { int input = 0; //< Iterate through each of the amplifiers and process the resulting code with the current phase setting for (int i = 0; i < NumAmplifiers; i++) { var phase = phaseSetting[i]; var amp = new Classes.Amplifier(intcodeSource); amp.AddInput(phase); amp.AddInput(input); amp.Process(); input = (int)amp.Output; } //< Then the 'final' value of 'input' is the final 'output' resultMap.Add(phaseSetting, input); } return(resultMap.Max(kvp => kvp.Value)); }
private static int GetMaximumFeedbackThrust(List <List <int> > phaseSettings, string intcodeSource) { //< Process each setting and get the result, retain all results var resultMap = new Dictionary <List <int>, int>(); foreach (var phaseSetting in phaseSettings) { //< First, make an array to house the Amplifiers var amps = new Classes.Amplifier[NumAmplifiers]; for (int i = 0; i < NumAmplifiers; i++) { amps[i] = new Classes.Amplifier(intcodeSource, feedbackLoop: true); //< Add the phase as the first input amps[i].AddInput(phaseSetting[i]); if (i == 0) { amps[i].AddInput(0); } } bool isFinished = false; while (!isFinished) { for (int i = 0; i < NumAmplifiers; i++) { //< Get the index of the Amplifier this one is connected to int connectedIdx = i < phaseSetting.Count - 1 ? i + 1 : 0; //< Process until Halt or next WaitingForInput amps[i].Process(); //< If we've got some new output, send it along if (amps[i].Output != null) { //< Update the current input to the last Output amps[connectedIdx].AddInput((int)amps[i].Output); } } isFinished = amps.Last().IsHalted; } var output = amps.Last().Output; //< Then the 'final' value of 'input' is the final 'output' resultMap.Add(phaseSetting, (int)output); } return(resultMap.Max(kvp => kvp.Value)); }