コード例 #1
0
ファイル: Part2.cs プロジェクト: E-Almqvist/adventofcode
        public int bruteforce(int[] input, int find, bool debug)
        {
            int noun, verb;

            int[] input_c;             // keep a version of the original
            int[] output = new int[input.Length];

            bool success = false;

            int min       = 0;          // min and max for the input values
            int max       = 16;         //
            int maxcombos = Convert.ToInt16(Math.Pow((double)(max + 1), 2.0));

            Part1.compile compile = new Part1.compile();

            Console.WriteLine("Bruteforcing...");
            for (int c = 0; c < max + 1; c++)
            {
                noun = Math.Clamp(c, min, max);
                // check every verb with c (the noun)
                for (int i = 0; i < max + 1; i++)
                {
                    input_c = input;                     // reset the intcode
                    verb    = Math.Clamp(i, min, max);

                    input_c[1] = noun;
                    input_c[2] = verb;

                    output = compile.intcode(input_c, debug);

                    if (output[0] == find)
                    {
                        Console.WriteLine("\n({0}) Found: {1}, {2}", find, noun, verb);
                        success = true;
                        return((100 * noun) + verb);
                    }
                }

                if (success == true)
                {
                    break;
                }
            }

            Console.WriteLine("");

            if (success == false)
            {
                Console.WriteLine("Nothing found :(");
                return(-1);
            }
            return(-1);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            string input = File.ReadAllText(@"./input.txt");             // get the input

            string[] inputs   = input.Split(",", StringSplitOptions.None);
            int[]    intcodes = Array.ConvertAll(inputs, str => int.Parse(str));              // convert all of the contents to int's

            int[] output = new int[intcodes.Length];

            Part1.compile compile = new Part1.compile();

            //// Part 1 stuff ////
            intcodes[1] = 12;
            intcodes[2] = 2;

            output = compile.intcode(intcodes, false);               // run the intcode program

            Console.WriteLine("\n--Intcode result--");
            for (int i = 0; i < output.Length; i++)
            {
                if (i != output.Length - 1)
                {
                    Console.Write(output[i].ToString() + ",");
                }
                else
                {
                    Console.Write(output[i].ToString() + "\n");
                }
            }
            Console.WriteLine("--End of Intcode result--\n");

            Console.WriteLine("Part 1 result: " + output[0].ToString());               // get the pos 0



            //// Part 2 stuff ////
            Part2.calcInputs calcInputs = new Part2.calcInputs();
            int[]            intcodes2  = intcodes; // make a new instance of it
            int res;

            res = calcInputs.bruteforce(intcodes2, 8, false);
            Console.WriteLine("Part 2 Result: {0}", res);
        }