예제 #1
0
    /// <summary>
    /// Returns the value of the bot that compares microchips with the specified values.
    /// </summary>
    /// <param name="instructions">The instructions to process.</param>
    /// <param name="a">The first number to find the bot that compares it.</param>
    /// <param name="b">The second number to find the bot that compares it.</param>
    /// <param name="binsOfInterest">The numbers of the bins of interest for which to find the product of the microchip values.</param>
    /// <returns>
    /// A named tuple that returns the number of the bot that compares microchips
    /// with the values specified by <paramref name="a"/> and <paramref name="b"/> and the product
    /// of the values of the microchips in the output bins with the numbers specified by <paramref name="binsOfInterest"/>.
    /// </returns>
    internal static (int Bot, int Product) GetBotNumber(IEnumerable <string> instructions, int a, int b, IEnumerable <int> binsOfInterest)
    {
        int max = Math.Max(a, b);
        int min = Math.Min(a, b);

        int botOfInterest           = -1;
        int productOfBinsOfInterest = 1;

        var processor = new InstructionProcessor();

        processor.Process(
            instructions,
            (bot, low, high) =>
        {
            if (low == min && high == max)
            {
                botOfInterest = bot;
            }
        });

        foreach (var bin in processor.Bins)
        {
            if (binsOfInterest.Contains(bin.Key))
            {
                productOfBinsOfInterest *= bin.Value.Microchip !.Value;
            }
        }

        return(botOfInterest, productOfBinsOfInterest);
    }
예제 #2
0
        /// <summary>
        /// Returns the value of the bot that compares microchips with the specified values.
        /// </summary>
        /// <param name="instructions">The instructions to process.</param>
        /// <param name="a">The first number to find the bot that compares it.</param>
        /// <param name="b">The second number to find the bot that compares it.</param>
        /// <param name="binsOfInterest">The numbers of the bins of interest for which to find the product of the microchip values.</param>
        /// <returns>
        /// A <see cref="Tuple{T1, T2}"/> that returns the number of the bot that compares microchips
        /// with the values specified by <paramref name="a"/> and <paramref name="b"/> and the product
        /// of the values of the microchips in the output bins with the numbers specified by <paramref name="binsOfInterest"/>.
        /// </returns>
        internal static Tuple<int, int> GetBotNumber(IEnumerable<string> instructions, int a, int b, IEnumerable<int> binsOfInterest)
        {
            int max = Math.Max(a, b);
            int min = Math.Min(a, b);

            int botOfInterest = -1;
            int productOfBinsOfInterest = 1;

            var processor = new InstructionProcessor();

            processor.Process(
                instructions,
                (bot, low, high) =>
                {
                    if (low == min && high == max)
                    {
                        botOfInterest = bot;
                    }
                });

            foreach (var bin in processor.Bins)
            {
                if (binsOfInterest.Contains(bin.Key))
                {
                    productOfBinsOfInterest *= bin.Value.Microchip.Value;
                }
            }

            return Tuple.Create(botOfInterest, productOfBinsOfInterest);
        }
        public void GetNumberOfValuesSent_GivenTestFile_Returns3()
        {
            string filename = @"Input\testb.txt";

            long expected = 3;

            long actual = InstructionProcessor.GetNumberOfValuesSent(filename);

            Assert.AreEqual(expected, actual);
        }
예제 #4
0
        public void should_result_divide_by_zero_exception()
        {
            // Arrange
            var filePath     = $"{Directory.GetCurrentDirectory()}\\InstructionSet\\DivideByZeroException.txt";
            var instructions = File.ReadAllText(filePath);

            var _sut = new InstructionProcessor();

            // Act & Assert
            Assert.Throws <DivideByZeroException>(() => _sut.Process(instructions));
        }
예제 #5
0
        public void should_throw_missing_field_exception_when_apply_instruction_not_found()
        {
            // Arrange
            var filePath     = $"{Directory.GetCurrentDirectory()}\\InstructionSet\\MissingApplyInstruction.txt";
            var instructions = File.ReadAllText(filePath);

            var _sut = new InstructionProcessor();

            // Act & Assert
            Assert.Throws <MissingFieldException>(() => _sut.Process(instructions));
        }
예제 #6
0
        public void should_result_zero_when_instructions_are_empty()
        {
            // Arrange
            int expectedResult = 0;
            var instructions   = string.Empty;

            var _sut = new InstructionProcessor();

            // Act
            var actualResult = _sut.Process(instructions);

            // Assert
            Assert.Equal(expectedResult, actualResult);
        }
예제 #7
0
        static void Main(string[] args)
        {
            CPU                  cpu       = new CPU();
            FileInputReader      reader    = new FileInputReader();
            InstructionProcessor processor = new InstructionProcessor(cpu, reader);

            processor.ProcessInstructions("Inputs/day-8.txt");

            // Part one
            Console.WriteLine(processor.GetCurrentLargestValue());

            // Part two
            Console.WriteLine(processor.GetHistoricalLargestValue());
        }
예제 #8
0
        public void should_successfully_process_instructions()
        {
            // Arrange
            int expectedResult = 16;
            var filePath       = $"{Directory.GetCurrentDirectory()}\\InstructionSet\\Sample.txt";
            var instructions   = File.ReadAllText(filePath);

            var _sut = new InstructionProcessor();

            // Act
            var actualResult = _sut.Process(instructions);

            // Assert
            Assert.Equal(expectedResult, actualResult);
        }
        public void ProcessInstructions()
        {
            CPU cpu = new CPU();
            StringInputReader    reader    = new StringInputReader();
            InstructionProcessor processor = new InstructionProcessor(cpu, reader);

            string input = @"b inc 5 if a > 1
                a inc 1 if b < 5
                c dec -10 if a >= 1
                c inc -20 if c == 10";

            processor.ProcessInstructions(input);

            Assert.Equal(1, processor.GetCurrentLargestValue());
            Assert.Equal(10, processor.GetHistoricalLargestValue());
        }
예제 #10
0
        private static void InstructionPass(MethodDefinition method, Predicate <MethodDefinition> selector, InstructionProcessor processor)
        {
            if (method.IsAbstract || method.Body == null || method.Body.Instructions == null || method.Body.Instructions.Count == 0)
            {
                return;
            }

            if (method.Body.CodeSize > 0 && selector(method))
            {
                Collection <SequencePoint> sequencePoints = method.DebugInformation.SequencePoints;

                int         pointIndex = 0;
                Instruction i          = method.Body.Instructions[0];

                while (i != null)
                {
                    SequencePoint point;
                    (point, pointIndex) = GetSequencePoint(sequencePoints, pointIndex, i);
                    i = processor(method, i, point);
                    i = i.Next;
                }
            }
        }
예제 #11
0
        private static void InstructionPass(TypeDefinition type, Predicate <MethodDefinition> selector, InstructionProcessor processor)
        {
            Collection <MethodDefinition> methods     = type.Methods;
            Collection <TypeDefinition>   nestedTypes = type.NestedTypes;

            for (int i = 0; i < methods.Count; i++)
            {
                InstructionPass(methods[i], selector, processor);
            }

            for (int i = 0; i < nestedTypes.Count; i++)
            {
                InstructionPass(nestedTypes[i], selector, processor);
            }
        }
예제 #12
0
 public static void ForEachInstruction(ModuleDefinition module, InstructionProcessor processor)
 {
     ForEachInstruction(module, method => true, processor);
 }
예제 #13
0
        private static void InstructionPass(MethodDefinition md, Predicate <MethodDefinition> selector, InstructionProcessor processor)
        {
            // process all references to replaced members with properties
            if (md.IsAbstract || md.Body == null || md.Body.Instructions == null)
            {
                return;
            }

            if (md.Body.CodeSize > 0 && selector(md))
            {
                var sequencePoints = md.DebugInformation.SequencePoints;

                var sequencePointIndex = 0;
                var instr = md.Body.Instructions[0];

                while (instr != null)
                {
                    SequencePoint sequencePoint;
                    (sequencePoint, sequencePointIndex) = GetSequencePoint(sequencePoints, sequencePointIndex, instr);
                    instr = processor(md, instr, sequencePoint);
                    instr = instr.Next;
                }
            }
        }
예제 #14
0
        private static void InstructionPass(TypeDefinition td, Predicate <MethodDefinition> selector, InstructionProcessor processor)
        {
            foreach (var md in td.Methods)
            {
                InstructionPass(md, selector, processor);
            }

            foreach (var nested in td.NestedTypes)
            {
                InstructionPass(nested, selector, processor);
            }
        }
예제 #15
0
        /// <summary>
        /// Executes a method for every instruction in a module
        /// </summary>
        /// <param name="module">The module to be passed over</param>
        /// <param name="selector">A predicate that indicates if we should pass over a method or not</param>
        /// <param name="processor">The function that processes each instruction</param>
        public static void ForEachInstruction(ModuleDefinition module, Predicate <MethodDefinition> selector, InstructionProcessor processor)
        {
            var types = new List <TypeDefinition>(module.Types);

            foreach (var td in types)
            {
                if (td.IsClass)
                {
                    InstructionPass(td, selector, processor);
                }
            }
        }
예제 #16
0
        private static void InstructionPass(MethodDefinition md, Predicate <MethodDefinition> selector, InstructionProcessor processor)
        {
            // process all references to replaced members with properties
            if (md.IsAbstract || md.Body == null || md.Body.Instructions == null)
            {
                return;
            }

            if (md.Body.CodeSize > 0 && selector(md))
            {
                Instruction instr = md.Body.Instructions[0];

                while (instr != null)
                {
                    instr = processor(md, instr);
                    instr = instr.Next;
                }
            }
        }
예제 #17
0
        public static void ForEachInstruction(ModuleDefinition module, Predicate <MethodDefinition> selector, InstructionProcessor processor)
        {
            Collection <TypeDefinition> types = module.Types;

            for (int i = 0; i < types.Count; i++)
            {
                InstructionPass(types[i], selector, processor);
            }
        }
예제 #18
0
 static void Main(string[] args)
 {
     InstructionProcessor instructionProcessor = new InstructionProcessor();
     instructionProcessor.Run();
     Console.ReadKey();
 }