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 panels = new Dictionary <(int X, int Y), int>(); (int X, int Y)currentPanel = (0, 0); (int X, int Y)newDirection = (0, 1); computer.Input.Add(0); ExitCode exitCode = computer.Run(); while (exitCode != ExitCode.SUCCESS) { var color = (int)computer.Output[computer.Output.Count - 2]; var dir = computer.Output.Last(); if (dir == 0) { newDirection = TransformLeft(newDirection); } else if (dir == 1) { newDirection = TransformRight(newDirection); } if (panels.ContainsKey(currentPanel)) { panels[currentPanel] = color; } else { panels.Add(currentPanel, color); } currentPanel.X += newDirection.X; currentPanel.Y += newDirection.Y; computer.Input.Add(panels.GetValueOrDefault(currentPanel, 0)); exitCode = computer.Run(); } Console.WriteLine($"the result for problem 1 is {panels.Count}"); }
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.Run(); }
static void Main(string[] args) { IntcodeComputer comp = new IntcodeComputer(); string input = File.ReadAllText("IntCode.txt"); //string input = File.ReadAllText("IntCodeTests.txt"); string[] programs = input.Split('\n'); foreach (string p in programs) { Console.WriteLine(p); for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { try { var program = comp.Parse(p); program[1] = i; program[2] = j; var result = comp.Run(program); Console.WriteLine(i + " " + j + " " + result[0]); if (result[0] == 19690720) { comp.Print(program); return; } } catch (Exception e) { // invalid input } } } Console.WriteLine(); } Console.ReadLine(); }
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}."); }
public static void RunDiagnosticTest(int code, string inputFilename) { long[] data = Utils.GetNumberInput(inputFilename, ","); var inputQueue = new BlockingCollection <long> { code }; var outputQueue = new BlockingCollection <long>(); IntcodeComputer computer = new IntcodeComputer(data, inputQueue, outputQueue); computer.Run().Wait(); var output = new List <long>(); while (!outputQueue.IsCompleted) { output.Add(outputQueue.Take()); } Console.WriteLine(string.Join(",", output)); }