public static void Problem2(string input) { var lines = Misc.ReadLines(input, Environment.NewLine); long[] inputValues = new List <string>(lines[0].Split(",", StringSplitOptions.RemoveEmptyEntries)).ConvertAll((string val) => long.Parse(val)).ToArray(); var computer = new IntcodeComputer(inputValues); int noun = 0; int verb = 0; bool found = false; for (; noun < 100 && !found; ++noun) { verb = 0; for (; verb < 100 && !found; ++verb) { inputValues[1] = noun; inputValues[2] = verb; computer.Programm = (long[])inputValues.Clone(); computer.Reset(); computer.Run(); if (computer.CurrentMemoryState()[0] == 19690720) { found = true; --noun; // it is raised by one at the end of the for loop --verb; // it is raised by one at the end of the for loop } } } if (found) { Console.WriteLine($"The result of problem 2 is {100 * noun + verb}"); } else { Console.WriteLine("No combination could be found."); } }
public static void Problem1(string input) { var lines = Misc.ReadLines(input, Environment.NewLine); long[] values = new List <string>(lines[0].Split(",", StringSplitOptions.RemoveEmptyEntries)).ConvertAll((string val) => long.Parse(val)).ToArray(); var computer = new IntcodeComputer(values); computer.InputMode = InputMode.Automatic; computer.OutputMode = OutputMode.Internal; var combinations = GetPermutations(new List <int>() { 0, 1, 2, 3, 4 }); long max = -1; foreach (var combi in combinations) { long output = 0; foreach (int i in combi) { computer.Programm = values; computer.Reset(); computer.Input = new List <long>() { i, output }; computer.Run(); output = computer.Output[0]; } if (output > max) { max = output; } } Console.WriteLine($"The result for problem 1 is {max}."); }