public async Task <long> RunProgram() { if (Completed) { return(ProgramOutput); } for (var ampCount = 0; ampCount < Amplifiers.Count; ++ampCount) { Amplifiers[ampCount].RunProgram(new IntcodeProgramInput(inputs: new[] { Phases[ampCount] })); } Amplifiers.First().AddInput(0); while (!Amplifiers.Last().Exited&& Amplifiers.All(amp => !amp.Errored)) { for (var ampCount = 0; ampCount < (Amplifiers.Count); ++ampCount) { var ampOutput = Amplifiers[ampCount].GetNextOutput(); if (!ampOutput.HasValue || Amplifiers[(ampCount + 1) % Amplifiers.Count].Exited) { continue; } Amplifiers[(ampCount + 1) % Amplifiers.Count].AddInput(ampOutput.Value); } } ProgramOutput = Amplifiers.Last().GetNextOutput().Value; Completed = true; return(ProgramOutput); }
public async Task Day_7_2(string inputFile, int answer) { var lines = LineReader.Open(inputFile); var amps = new Amplifiers(new[] { 5, 6, 7, 8, 9 }); var x = await amps.FindMaxOutput(lines, feedbackLoop : true); Assert.Equal(answer, x); }
public async Task Day_7_1(string inputFile, int answer) { var lines = LineReader.Open(inputFile); var amps = new Amplifiers(new[] { 0, 1, 2, 3, 4 }); var x = await amps.FindMaxOutput(lines); Assert.Equal(answer, x); }
public void CanCalcMaxFinal() { int[] memory1 = { 3, 8, 1001, 8, 10, 8, 105, 1, 0, 0, 21, 46, 67, 76, 97, 118, 199, 280, 361, 442, 99999, 3, 9, 1002, 9, 3, 9, 101, 4, 9, 9, 102, 3, 9, 9, 1001, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 99, 3, 9, 102, 2, 9, 9, 101, 5, 9, 9, 1002, 9, 2, 9, 101, 2, 9, 9, 4, 9, 99, 3, 9, 101, 4, 9, 9, 4, 9, 99, 3, 9, 1001, 9, 4, 9, 102, 2, 9, 9, 1001, 9, 4, 9, 1002, 9, 5, 9, 4, 9, 99, 3, 9, 102, 3, 9, 9, 1001, 9, 2, 9, 1002, 9, 3, 9, 1001, 9, 3, 9, 4, 9, 99, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1001, 9,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 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 99 }; var memoryList = memory1.ToArray(); OutputList = new List <int>(); var res = Amplifiers.calcMax(memory1, CollectOutput); Assert.That(res.Equals(67023)); }
public async Task BestPhaseSettings(int maxOutput, string program, int[] phaseSettings) { var amps = new Amplifiers(new[] { 0, 1, 2, 3, 4 }); var prog = _2.IntComputer.ParseLine(program); var(output, ps) = await amps.FindBestPhaseSetting(prog); Assert.Equal(maxOutput, output); Assert.Equal(phaseSettings.Length, ps.Length); for (int i = 0; i < ps.Length; i++) { Assert.Equal(phaseSettings[i], ps[i]); } }
public void CanCalcMax3() { int[] memory1 = { 3, 31, 3, 32, 1002, 32, 10, 32, 1001, 31, -2, 31, 1007, 31, 0, 33, 1002, 33, 7, 33, 1, 33, 31, 31, 1, 32, 31, 31, 4, 31, 99, 0,0, 0 }; var memoryList = memory1.ToArray(); OutputList = new List <int>(); var res = Amplifiers.calcMax(memory1, CollectOutput); Assert.That(res.Equals(65210)); }
public void CanCalcMax2() { int[] memory1 = { 3, 23, 3, 24, 1002, 24, 10, 24, 1002, 23, -1, 23, 101, 5, 23, 23, 1, 24, 23, 23, 4, 23, 99, 0, 0 }; var memoryList = memory1.ToArray(); OutputList = new List <int>(); var res = Amplifiers.calcMax(memory1, CollectOutput); Assert.That(res.Equals(54321)); }
public void CanCalcMax1() { int[] memory1 = { 3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0 }; var memoryList = memory1.ToArray(); OutputList = new List <int>(); var phases = new int[] { 4, 3, 2, 1, 0 }; var res = Amplifiers.calcMax(memory1, CollectOutput); Assert.That(res.Equals(43210)); }
public void CanCalc5Amps() { int[] memory1 = { 3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0 }; var memoryList = memory1.ToArray(); OutputList = new List <int>(); var phases = new int[] { 4, 3, 2, 1, 0 }; var res = Amplifiers.Calc5AmpsPhase1(memory1, phases, CollectOutput); //Assert.That(res.Equals(3)); }
public void Actual2() { output.WriteLine($"Answer: {string.Join(", ", Amplifiers.FindBest2(new int[] { 3, 8, 1001, 8, 10, 8, 105, 1, 0, 0, 21, 42, 67, 84, 109, 126, 207, 288, 369, 450, 99999, 3, 9, 102, 4, 9, 9, 1001, 9, 4, 9, 102, 2, 9, 9, 101, 2, 9, 9, 4, 9, 99, 3, 9, 1001, 9, 5, 9, 1002, 9, 5, 9, 1001, 9, 5, 9, 1002, 9, 5, 9, 101, 5, 9, 9, 4, 9, 99, 3, 9, 101, 5, 9, 9, 1002, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 99, 3, 9, 1001, 9, 2, 9, 102, 4, 9, 9, 101, 2, 9, 9, 102, 4, 9, 9, 1001, 9, 2, 9, 4, 9, 99, 3, 9, 102, 2, 9, 9, 101, 5, 9, 9, 1002, 9, 2, 9, 4, 9, 99, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 99, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 99, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 99, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 99, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 99 }))}"); }
public void Examples2(int[] sequence, int[] program, int expected) { Assert.Equal(expected, Amplifiers.Run2(sequence, program)); }