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); }
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); }
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]); }