コード例 #1
0
        private int FindNounAndVerb(IList <int> initialMemory)
        {
            IList <int> memory = ListUtil.DeepCopy(initialMemory);
            int         noun   = 0;
            int         verb   = 0;

            bool validNounAndVerb = false;

            while (!validNounAndVerb)
            {
                for (int i = 0; i < 100 && !validNounAndVerb; i++)
                {
                    for (int j = 0; j < 100 && !validNounAndVerb; j++)
                    {
                        noun      = i;
                        verb      = j;
                        memory[1] = noun;
                        memory[2] = verb;
                        IList <int> finalMemory = intcodeCPU.ApplyOperations(memory);
                        if (finalMemory[0] == 19690720)
                        {
                            validNounAndVerb = true;
                        }
                    }
                }
            }

            return(100 * noun + verb);
        }
コード例 #2
0
ファイル: IntcodeCPU.cs プロジェクト: Josh961/Advent-of-Code
        public IList <int> ApplyOperations(IList <int> initialMemory)
        {
            IList <int> memory = ListUtil.DeepCopy(initialMemory);

            int instructionPointer = 0;

            for (int i = 0; memory[i] != 99; i += instructionPointer)
            {
                int instruction    = memory[i];
                int parameterOne   = memory[i + 1];
                int parameterTwo   = memory[i + 2];
                int parameterThree = memory[i + 3];

                switch (instruction)
                {
                case 1:
                    int additionResult = memory[parameterOne] + memory[parameterTwo];
                    memory[parameterThree] = additionResult;
                    instructionPointer     = 4;
                    break;

                case 2:
                    int multiplicationResult = memory[parameterOne] * memory[parameterTwo];
                    memory[parameterThree] = multiplicationResult;
                    instructionPointer     = 4;
                    break;
                }
            }

            return(memory);
        }
コード例 #3
0
        private int RestoreGravityAssist(IList <int> initialMemory)
        {
            IList <int> memory = ListUtil.DeepCopy(initialMemory);
            int         noun   = 12;
            int         verb   = 2;

            memory[1] = noun;
            memory[2] = verb;
            IList <int> finalMemory = intcodeCPU.ApplyOperations(memory);

            return(finalMemory[0]);
        }