コード例 #1
0
        public int ProcessInstruction(string command, Coprocessor coprocessor)
        {
            string[] commandSplit = command.Split(' ');
            switch (commandSplit[0])
            {
            case "set":
                DetermineValue(commandSplit[1], coprocessor);
                coprocessor.Registers[commandSplit[1]] = DetermineValue(commandSplit[2], coprocessor);
                break;

            case "sub":
                DetermineValue(commandSplit[1], coprocessor);
                coprocessor.Registers[commandSplit[1]] -= DetermineValue(commandSplit[2], coprocessor);
                break;

            case "mul":
                DetermineValue(commandSplit[1], coprocessor);
                coprocessor.Registers[commandSplit[1]] = coprocessor.Registers[commandSplit[1]] * DetermineValue(commandSplit[2], coprocessor);
                return(1);

            case "jnz":
                long firstValue = DetermineValue(commandSplit[1], coprocessor);
                if (firstValue != 0)
                {
                    coprocessor.CurrentInstruction += (int)DetermineValue(commandSplit[2], coprocessor) - 1;
                }
                break;

            default:
                throw new ArgumentException("Unknown command");
            }

            return(0);
        }
コード例 #2
0
        private long FindRegisterValueOrCreateNew(string key, Coprocessor coprocessor)
        {
            if (coprocessor.Registers.ContainsKey(key))
            {
                return(coprocessor.Registers[key]);
            }

            coprocessor.Registers.Add(key, 0);
            return(0);
        }
コード例 #3
0
        private long DetermineValue(string value, Coprocessor coprocessor)
        {
            long result;

            if (long.TryParse(value, out result))
            {
                return(result);
            }

            long found = FindRegisterValueOrCreateNew(value, coprocessor);

            return(found);
        }
コード例 #4
0
        public int RunThroughInstructionsAndCountMult(string filePath)
        {
            List <string> instructions = GetInstructions(filePath);

            Coprocessor coprocessor = new Coprocessor();
            int         count       = 0;

            do
            {
                string instruction = instructions[coprocessor.CurrentInstruction];
                count += ProcessInstruction(instruction, coprocessor);
                coprocessor.CurrentInstruction++;
            } while (coprocessor.CurrentInstruction < 32);  // 32 is lines in current program

            return(count);
        }