Example #1
0
        public List <int> getUpdatedIntCode(string intCode, string inp)
        {
            int        position = 0;
            int        input    = int.Parse(inp);
            List <int> IntCode  = intCode.Length > 2
       ? new List <string>(intCode.Split(",")).Select(int.Parse).ToList()
       : RawData.dataForAdvent5();

            while (position < IntCode.Count)
            {
                string abcde = IntCode[position].ToString();
                abcde = "00000".Substring(abcde.Length) + abcde;
                int opCode = int.Parse(abcde.Substring(3, 2));
                if (opCode == 99)
                {
                    break;
                }
                int qnty = opCode < 3 ? Math.Min(4, IntCode.Count - position) : 2;
                var aSet = IntCode.GetRange(position, qnty);
                position += qnty;
                if (opCode > 2)
                {
                    if (opCode == 3)
                    {
                        IntCode[aSet[1]] = input;
                    }
                    if (opCode == 4)
                    {
                        Console.WriteLine("output -  " + IntCode[aSet[1]]);
                    }
                }
                else if (aSet.Count > 3)
                {
                    int p1 = abcde.Substring(2, 1) == "1" ? aSet[1] : IntCode[aSet[1]];
                    int p2 = abcde.Substring(1, 1) == "1" ? aSet[2] : IntCode[aSet[2]];
                    int p3 = abcde.Substring(0, 1) == "1" ? aSet[3] : IntCode[aSet[3]];
                    if (opCode == 1)
                    {
                        IntCode[aSet[3]] = p1 + p2;
                    }
                    if (opCode == 2)
                    {
                        IntCode[aSet[3]] = p1 * p2;
                    }
                }
            }
            return(IntCode);
        }
Example #2
0
        public string getUpdatedIntCode(string intCode, string inp)
        {
            int        position = 0;
            int        input    = int.Parse(inp);
            string     output   = null;
            List <int> IntCode  = intCode.Length > 2
       ? new List <string>(intCode.Split(",")).Select(int.Parse).ToList()
       : RawData.dataForAdvent5();

            while (position < IntCode.Count)
            {
                string abcde = IntCode[position].ToString();
                abcde = "00000".Substring(abcde.Length) + abcde;
                int opCode = int.Parse(abcde.Substring(3, 2));
                if (opCode == 99)
                {
                    break;
                }
                int qnty = (opCode == 3 || opCode == 4) ? 2
            : ((opCode == 5 || opCode == 6) ? 3 : Math.Min(4, IntCode.Count - position));
                var aSet = IntCode.GetRange(position, qnty);
                position += qnty;
                if (opCode == 3 && aSet.Count > 1)
                {
                    IntCode[aSet[1]] = input;
                }
                if (opCode == 4 && aSet.Count > 1 && IntCode.Count > aSet[1])
                {
                    output = IntCode[aSet[1]].ToString(); Console.WriteLine("output -  " + output);
                }
                if (aSet.Count > 2)
                {
                    int p1 = abcde.Substring(2, 1) == "1" ? aSet[1] : IntCode[aSet[1]];
                    int p2 = abcde.Substring(1, 1) == "1" ? aSet[2] : IntCode[aSet[2]];
                    if (opCode == 5 && aSet.Count > 2 && p1 > 0)
                    {
                        position = p2;
                    }
                    if (opCode == 6 && aSet.Count > 2 && p1 == 0)
                    {
                        position = p2;
                    }
                    if (aSet.Count > 3)
                    {
                        int p3 = abcde.Substring(0, 1) == "1" ? aSet[3] : IntCode[aSet[3]];
                        if (opCode == 7 && aSet.Count > 3)
                        {
                            if (p1 < p2)
                            {
                                IntCode[aSet[3]] = 1;
                            }
                            else
                            {
                                IntCode[aSet[3]] = 0;
                            }
                        }
                        if (opCode == 8 && aSet.Count > 3)
                        {
                            if (p1 == p2)
                            {
                                IntCode[aSet[3]] = 1;
                            }
                            else
                            {
                                IntCode[aSet[3]] = 0;
                            }
                        }
                        if (opCode < 3 && aSet.Count > 3)
                        {
                            if (opCode == 1)
                            {
                                IntCode[aSet[3]] = p1 + p2;
                            }
                            if (opCode == 2)
                            {
                                IntCode[aSet[3]] = p1 * p2;
                            }
                        }
                    }
                }
            }
            return(output);
        }
Example #3
0
        public string getUpdatedIntCode(string intCodeFromConsole, string inp)
        {
            int        userInput = int.Parse(inp);
            int        output    = 0;
            List <int> instructionParams;
            List <int> IntCode = getIntCode();
            int        nextInstructionStartPoint = 0;
            string     nextInstruction;
            int        operationCode;

            while (nextInstructionStartPoint < IntCode.Count)
            {
                nextInstruction = getNextInstruction();
                operationCode   = int.Parse(nextInstruction.Substring(3, 2));
                if (operationCode == 99)
                {
                    break;
                }

                int digitsQnty = getNextInstructionDigitsQuantity();

                instructionParams = IntCode.GetRange(nextInstructionStartPoint, digitsQnty);

                nextInstructionStartPoint += digitsQnty;

                int p1 = getParameterOneDependingOnMode();
                int p2 = instructionParams.Count > 2 ? getParameterTwoDependingOnMode() : 0;
                int p3 = instructionParams.Count > 3 ? instructionParams[3] : 0;

                switch (OpCodes[operationCode - 1])
                {
                case "opCodeRequires__InputAtP3sumOfP1nP2":
                    IntCode[p3] = p1 + p2;
                    break;

                case "opCodeRequires__InputAtP3productOfP1nP2":
                    IntCode[p3] = p1 * p2;
                    break;

                case "opCodeRequires__InputAtPosition":
                    IntCode[instructionParams[1]] = userInput;
                    break;

                case "opCodeRequires__DisclosingOutput":
                    output = IntCode[instructionParams[1]];
                    Console.WriteLine("output -  " + output);
                    break;

                case "opCodeRequires__JumpIf_P1isNotZero":
                    if (p1 > 0)
                    {
                        nextInstructionStartPoint = p2;
                    }
                    break;

                case "opCodeRequires__JumpIf_P1isZero":
                    if (p1 == 0)
                    {
                        nextInstructionStartPoint = p2;
                    }
                    break;

                case "ifP1lessThanP2storeOneElseZeroAtP3":
                    if (p1 < p2)
                    {
                        IntCode[p3] = 1;
                    }
                    else
                    {
                        IntCode[p3] = 0;
                    }
                    break;

                case "ifP1equalsP2storeOneElseZeroAtP3":
                    if (p1 == p2)
                    {
                        IntCode[p3] = 1;
                    }
                    else
                    {
                        IntCode[p3] = 0;
                    }
                    break;

                default:
                    Console.WriteLine("Something wrong !");
                    break;
                }
            }
            return(output.ToString());

            // local (in function) functions
            List <int> getIntCode()
            {
                return(intCodeFromConsole.Length > 2
        ? intCodeFromConsole.Split(",").Select(int.Parse).ToList()
        : RawData.dataForAdvent5());
            }

            string getNextInstruction()
            {
                string abcde = IntCode[nextInstructionStartPoint].ToString();

                return("00000".Substring(abcde.Length) + abcde);
            }

            int getNextInstructionDigitsQuantity()
            {
                return((operationCode == 3 || operationCode == 4)
            ? 2 : ((operationCode == 5 || operationCode == 6)
            ? 3 : Math.Min(4, IntCode.Count - nextInstructionStartPoint)));
            }

            int getParameterOneDependingOnMode()
            {
                string p1mode = nextInstruction.Substring(2, 1);

                return(p1mode == "1" ? instructionParams[1] : IntCode[instructionParams[1]]);
            }

            int getParameterTwoDependingOnMode()
            {
                string p2mode = nextInstruction.Substring(1, 1);

                return(p2mode == "1" ? instructionParams[2] : IntCode[instructionParams[2]]);
            }
        }