Пример #1
0
        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();
        }
Пример #2
0
        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.");
            }
        }
Пример #3
0
        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}.");
        }
Пример #4
0
        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));
        }